Python
Python
A JOKE-TELLING PROGRAM
This chapter’s program tells a few jokes to the user and demonstrates more advanced
ways to use strings with the print() function. Most of the games in this book will have
simple text for input and output. The input is typed on the keyboard by the user, and
the output is the text displayed on the screen.
TOPICS COVERED IN THIS CHAPTER
• Escape characters
• Using single quotes and double quotes for strings
• Using print()’s end keyword parameter to skip newlines
You’ve already learned how to display simple text output with the print() function.
Now let’s take a deeper look at how strings and print() work in Python.
Knock knock.
Who's there?
Interrupting cow.
Interrupting cow wh-MOO!
Source Code for Jokes
Open a new file editor window by clicking File New Window. In the blank window
that appears, enter the source code and save it as jokes.py. Then run the program by
pressing F5.
If you get errors after entering this code, compare the code you typed to the book’s
code with the online diff tool at https://www.nostarch.com/inventwithpython#diff.
jokes.py
Lines 1 and 3 use the print() function call to ask and give the answer to the first
joke. You don’t want the user to immediately read the joke’s punchline, so there’s a
call to the input() function after the first print() instance. The user will read the joke,
press ENTER, and then read the punchline.
The user can still type in a string and press ENTER, but this returned string isn’t
being stored in any variable. The program will just forget about it and move to the
next line of code.
The last print() function call doesn’t have a string argument. This tells the program
to just print a blank line. Blank lines are useful to keep the text from looking crowded.
Escape Characters
Lines 5 to 8 print the question and answer to the next joke:
On line 5, there’s a backslash right before the single quote: \'. (Note that \ is a
backslash, and / is a forward slash.) This backslash tells you that the letter right after it
is an escape character. An escape character lets you print special characters that are
difficult or impossible to enter into the source code, such as the single quote in a
string value that begins and ends with single quotes.
In this case, if we didn’t include the backslash, the single quote in astronaut\'s would
be interpreted as the end of the string. But this quote needs to be part of the string.
The escaped single quote tells Python that it should include the single quote in the
string.
But what if you actually want to display a backslash?
Switch from your jokes.py program to the interactive shell and enter
this print() statement:
But you cannot mix quotes. This line will give you an error because it uses both
quote types at once:
>>> 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."
You use single quotes to surround the string, so you need to add a backslash before
the single quote in Abe\'s. But the double quotes in "Sure." don’t need backslashes. 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.
Now check out another example:
>>> print("She said, \"I can't believe you let them borrow your car.\"")
She said, "I can't believe you let them borrow your car."
The string is surrounded in double quotes, so you need to add backslashes for all of
the double quotes within the string. You don’t need to escape the single quote in can't.
To summarize, in the single-quote strings, you don’t need to escape double quotes
but do need to escape single quotes, and in the double-quote strings, you don’t need to
escape single quotes but do need to escape double quotes.
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 argument in line 15’s print() function?
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 print() can optionally have a second
parameter: end.
Remember that an argument is the value passed in a function call. The blank string
passed to print() is called a keyword argument. The end in end='' is called a keyword
parameter. To pass a keyword argument to this keyword parameter, you must
type end= before it.
When we run this section of code, the output is
Knock knock.
Who's there?
Interrupting cow.
Interrupting cow wh-MOO!
Because we passed a blank string to the end parameter, the print() function will add
a blank string instead of adding a newline. This is why '-MOO!' appears next to the
previous line, instead of on its own line. There was no newline after the 'Interrupting cow
wh' string was printed.
Summary
This chapter explores the different ways you can use the print() function. Escape
characters are used for characters that are difficult to type into the code with the
keyboard. If you want to use special characters in a string, you must use a backslash
escape character, \, followed by another letter for the special character. For example, \
n would be a newline. If your special character is a backslash itself, you use \\.
The print() function automatically appends a newline character to the end of a
string. Most of the time, this is a helpful shortcut. But sometimes you don’t want a
newline character. To change this, you can pass a blank string as the keyword
argument for print()’s end keyword parameter. For example, to print spam to the screen
without a newline character, you would call print('spam', end='').