Introduction to Python
  • Introduction
  • Preface
  • Background
  • Installing Python
  • Getting Started
  • Basics
    • More Printing
    • Strings
    • Numbers
    • Exercises
    • Comments
  • Variables
    • Operators
    • Type Conversion
    • Exercises
    • A Brief Introduction to Lists
    • Game Exercise
  • Human Input
  • Functions
    • First Functions
    • Why Functions
    • Exercises
  • Indentation
  • Decisions
    • Booleans
    • Logical Operators
    • If Statements
    • Elif and Else Statements
    • Exercises
    • Rock, Paper, Scissors
    • Game Exercise
    • Game Exercise 2
  • Lists
  • Loops
    • For Loops
    • While Loops
  • More Data Structures
    • Tuples
    • Dictionaries
  • Pygame
  • Extra Content
    • Computers and Code
    • More About Python
    • For Loops with Range
    • List Slicing
Powered by GitBook
On this page
  1. Basics

More Printing

Well, with the same Python shell we used in the last section, let's print some more things.

print('Roses are red')
print('Violets are blue')
print('Sugar is sweet')
print('And so are you')

As Python processes code line by line, the output comes out line by line. Think you can put them all together and get one big output? Of course you can! Let's try:

print('Roses are red Violets are blue Sugar is sweet And so are you')

Well that's not exactly what we expected, we got this for output

Roses are red Violets are blue Sugar is sweet And so are you

Let's format it so that it'll print out more like how we expect poems to be written.

print('Roses are red\nViolets are blue\nSugar is sweet\nAnd so are you')

The output should be like this:

Roses are red
Violets are blue
Doubles with heavy pepper
Will never be sweet for you

You would have noticed that instead of a space we use \n. That's a newline character, it's used in strings to output to, unsuprisinly, a new line.

PreviousBasicsNextStrings

Last updated 6 years ago