Python 3 Print and Format Flashcards

 

card 1 of 22

 

print("Hello")

 

common sense

Hello
(but notice that it is not "Hello")

card 2 of 22

 

print("\tHello")

 

the "\t" is a symbolic code for a tab

     Hello

card 3 of 22

 

"\tHello"

 

not a print statement, just normal evaluation

'\tHello'

card 4 of 22

 

"\tHello"[:2]

 

remember how slice/index works with strings

'\tH'

card 5 of 22

 

print("\tHello"[2:])

 

like previous question, but

     H
(but notice quotes are removed)

card 6 of 22

 

print("Greetings","Earthlings")

 

comma separates items to be printed

Greetings Earthlings

card 7 of 22

 

print(("Greetings","Earthlings"))

 

comma separates items in tuple

('Greetings','Earthlings')

card 8 of 22

 

print("Greetings" + "Earthlings")

 

evaluate expressions before printing

GreetingsEarthlings

card 9 of 22

 

print("Greetings\nEarthlings")

 

the "\n" is a code for making a new line

Greetings
Earthlings

card 10 of 22

 

"Greetings\nEarthlings"

 

the "\n" is a code for making a new line

'Greetings\nEarthlings'

card 11 of 22

 

print(list("abc"))

 

remember what list conversion does with strings

['a','b','c']

card 12 of 22

 

print(range(3),range(2),True)

 

each item is evaluated first, before printing

[0,1,2] [0,1] True
(notice no commas between items output)

card 13 of 22

 

print("The symbol for three is", 2+1, ".")

 

each item is evaluated first, before printing

The symbol for three is 3 .

card 14 of 22

 

"{0}".format(3)

 

the {0} will be substituted by the format method

'3'

card 15 of 22

 

str(3)=="{0}".format(3)

 

actually, common sense after evaluation

True

card 16 of 22

 

"XX{0}XX".format(3)

 

the {0} is substituted by format argument

'XX3XX'

card 17 of 22

 

"XX{0}XX".format("abc")

 

the {0} is substituted by format argument

'XXabcXX'

card 18 of 22

 

"XX{0}XX".format([2,4,6])

 

the {0} is substituted by format argument

'XX[2,4,6]XX'

card 19 of 22

 

print("The symbol for three is {0}.".format(2+1))

 

the argument is evaluated first, before printing

The symbol for three is 3.

card 20 of 22

 

"{0} + {1} equals {2}".format(2,2,4)

 

multiple substitutions fill in by number

'2 + 2 equals 4'

card 21 of 22

 

"{2} + {1} equals {0}".format(2,2,4)

 

multiple substitutions fill in by number

'4 + 2 equals 2'

card 22 of 22

 

"{0} + {1} equals {2}".format("two","two","four")

 

multiple substitutions fill in by number

'two + two equals four'

 

End of Problem Set

(use browser reload to restart this problem set)