So far, we've only seen one way to use print
: we go print(<value>)
, and get the value printed to the screen. What if we want to print several things? We can go
print("ab," + "cd")
We are still printing one value there, it's just that the value "ab,cd"
is computed before printing it by adding together (the term is concatenating) ths strings "ab,"
and "cd"
.
Here is another way to do this:
print("ab,", "cd")
We used a comma rather than a a plus there: we just told print
to do two things: print "ab,"
, and then print "cd"
. Note that print
inserted a space in between the two strings.
This is especially useful when we want to print both numbers and strings. The following doesn't work:
print("abc" + 123)
We cannot compute "abc" + 123
, since you can't add a string and an integer: it doesn't make sense!. But there is no problem with asking print
to print two things: first "abc"
, and then 123
(and also insert a space in between)
print("abc", 123)
Note that the comma there is not printed. To print a comma, do what you would do to print any character, and put the comma inside quotes.
print("abc;", 123)
print("abc,", 123)