Game Development Reference
In-Depth Information
I like to use single quotes because I don't have to hold down the shift key on the
keyboard to type them. It's easier to type, and the computer doesn't care either way.
But remember, just like you have to use the escape character \' to have a single quote in a
string surrounded by single quotes, you need the escape character \" to have a double quote
in a string surrounded by double quotes. For example, look at these two lines:
>>> print('I asked to borrow Abe\'s car for a
week. He said, "Sure."')
I asked to borrow Abe's car for a week. He said,
"Sure."
>>> print("He said, \"I can't believe you let him
borrow your car.\"")
He said, "I can't believe you let him borrow your
car."
Did you notice that in the single quote strings you do not need to escape double quotes,
and in the double quote strings you do not need to escape single quotes? The Python
interpreter is smart enough to know that if a string starts with one type of quote, the other
type of quote doesn't mean the string is ending.
The end Keyword Argument
9. print('Knock knock.')
10. input()
11. print("Who's there?")
12. input()
13. print('Interrupting cow.')
14. input()
15. print('Interrupting cow wh', end='')
16. print('-MOO!')
Did you notice the second parameter on line 15's print() ? Normally, print() adds
a newline character to the end of the string it prints. (This is why a blank print()
function will just print a newline.) But the print() function can optionally have a second
parameter (which has the name end.) The blank string we are passing is called a keyword
argument . The end parameter has a specific name, and to pass an argument to this
specific parameter we need to use the end= syntax.
Notice that when you type the keyword and the keyword argument, you use only one =
sign. It is end='' , and not end=='' .
By passing a blank string for the end we tell the print() function to not add a newline
at the end of the string, but instead add a blank string. This is why '-MOO!' appears next
Search WWH ::




Custom Search