Python 2 Print and Format Flashcards

 

card 1 of 19

 

print "Hello"

 

common sense

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

card 2 of 19

 

print "\tHello"

 

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

     Hello

card 3 of 19

 

"\tHello"

 

not a print statement, just normal evaluation

'\tHello'

card 4 of 19

 

"\tHello"[:2]

 

remember how slice/index works with strings

'\tH'

card 5 of 19

 

print "\tHello"[2:]

 

like previous question, but

     H
(but notice quotes are removed)

card 6 of 19

 

print "Greetings", "Earthlings"

 

comma separates items to be printed

Greetings Earthlings

card 7 of 19

 

print "(Greetings", "Earthlings")

 

comma separates items in tuple

('Greetings','Earthlings')

card 8 of 19

 

print "Greetings" + "Earthlings"

 

evaluate expressions before printing

GreetingsEarthlings

card 9 of 19

 

print "Greetings\nEarthlings"

 

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

Greetings
Earthlings

card 10 of 19

 

"Greetings\nEarthlings"

 

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

'Greetings\nEarthlings'

card 11 of 19

 

print list("abc")

 

remember what list conversion does with strings

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

card 12 of 19

 

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 19

 

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

 

each item is evaluated first, before printing

The symbol for three is 3 .

card 14 of 19

 

"%d" % 3

 

the % is an operator with a string on the left

'3'

card 15 of 19

 

str(3)==("%d"%3)

 

actually, common sense after evaluation

True

card 16 of 19

 

"XX%dXX" % 3

 

the % is an operator with a string on the left

'XX3XX'

card 17 of 19

 

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

 

each item is evaluated first, before printing

The symbol for three is 3.

card 18 of 19

 

"%d + %d equals %d" % (2,2,4)

 

multiple substitutions fill in left-to-right

'2 + 2 equals 4'

card 19 of 19

 

"%s + %s equals %s" % ("two","two","four")

 

whereas %d is for integers, %s is for strings

'two + two equals four'

 

End of Problem Set

(use browser reload to restart this problem set)