Chapter 9-
How to Make Your Program Interactive
Inputs ()
So far, we’ve only been writing programs that only use data we have explicitly defined in the script. However, your applications can also take in input from a user and utilize it. Python lets us solicit contributions from the user with a very intuitively named function - the input() function. Using the input() function enables us to prompt the user to enter information, which we can further manipulate. For example, we can take the user’s input and save it as a variable, print it straight to the terminal, or do anything else we might like.
When we use the input function, we can pass in a string. The user will see this string as a prompt, and their response to the prompt will be saved as the input value. For instance, if we wanted to query the user for their favorite food, we could write the following:
favorite_food = input("What is your favorite food?: ")
If you ran this code example, you would be prompted to input your favorite food. You could save multiple variables this way and print them all at once using the print() function along with print formatting, as we covered earlier. To be clear, the text that you write in the input function is what the user will see as a prompt; it isn’t what you are inputting into the system as a value.
When you run the code above, you’ll be prompted for an input. After you type in some text and hit the enter/return key, the text you wrote will be stored as the variable favorite_food. The input command can be used along with string formatting to inject variables into the text prompt that the user will see. For instance, if we had a variable called user_name that stored the name of the user, we could structure the input statement like this:
favorite_food = input("What is {}’s favorite food?: ").format("user name here")
Print ()
In order to prevent a long string from running across the screen, we
can use triple quotes to surround our string. Printing with triple quotes allows us to separate our print statements onto multiple lines. For example, we could print like this:
print('''By using triple quotes we can
Divide our print statement onto multiple
Lines, making it easier to read.''')
Formatting the print statement like that will give us:
By using triple quotes we can
Divide our print statement onto multiple
Lines, making it easier to read.
What if we need to print characters that are equivalent to string formatting instructions? For example, if we ever needed to print out the figures “%s“or “%d“, we would run into trouble. If you recall, these are string formatting commands, and if we try to print these out, the interpreter will interpret them as formatting commands.
Here’s a practical example. As mentioned, typing “/t” in our string will put a tab in the middle of our line. Assume we type the following:
Print ("We want a \t here, not a tab.")
We’d get back this:
We want a here, not a tab.
By using an escape character, we can tell Python to include the characters that come next as part of the string’s value. The escape character we want to use is the “raw string” character, an “r” before the first quote in a series, like this:
print(r"We want a \t here, not a tab.")
So, if we used the raw string, we’d get the format we want back:
We want a \t here, not a tab.
The “raw string” formatter enables you to put any combination of characters you’d like within the string and have it be considered as part of the string’s value.
However, what if we did want the tab in the middle of our string? In
that case, using special formatting characters in our string is referred to as using “escape characters.” “Escaping” a string is a method of reducing the ambiguity in how characters are interpreted. When we use an escape character, we escape the typical way that Python uses to explain certain aspects, and the roles we type are understood to be part of the string’s value. The escape primarily used in Python is the backslash (\). The backslash prompts Python to listen for a unique character to follow that will be translated to a specific string formatting command.
We already saw that using the “\t” escape character puts a tab in the middle of our string, but there are other escape characters we can use as well and they are shown below:
\n - Starts a new line
\\- Prints out a backslash itself
\" - Prints out a double quote instead of a double quote marking the end of a string
\' - Like above, but prints out a single quote
Input and Formatting Exercise
Let’s do another exercise that applies what we’ve covered in this section. You should try to write a program that does the following:
- Prompts the user for answers to several different questions
- Prints out the answers on different lines using a single print statement
Give this a shot before you look below for a solution to this exercise prompt.
If you’ve given this a shot, your answer might look something like this:
favorite_food = input("What's your favorite food? :")
favorite_animal = input("What about your favorite animal? :")
favorite_movie = input("What's the best movie? :")
print("Favorite food is: " + favorite_food + "\n" +
"Favorite animal is: " + favorite_animal + "\n" +
"Favorite movies is: " + favorite_movie)
Triple Quotes
If we want displaying a long message with the use of the print function, we may use the triple-quotes symbol (“or”) in order to span over multiple lines in our message.
Example:
print ('''Hi World.
My name is Christopher and
I am 21 years old.''')
This will give us:
Hi World.
My name is Christopher and
I am 21 years old.
This help and increase the readability of the message.
Escape Characters
Sometimes, we also need to have some special “unprintable” characters printed. In order to do that, we need the \ (backslash) character to escape the characters that have different meaning otherwise.
Example, in printing a tab, we will type the backlash character before letter t: \t. if the \ character will be removed, the letter t will be printed. By doing that, a tab will be printed, in other words, if you will type print (‘Hi\tWorld’) you’ll get Hi World
The other uses of the backlash character will be shown:
>>> will show the command & the next lines be showing the output.
\n (Prints a newline)
>>> print ('Hello\nWorld')
Hello
World
\\ (Prints the backslash character itself)
>>> print ('\\')
\
\" (Prints double quote, so that the double quote does not signal the end of the string)
>>> print ("I am 5'9\" tall")
I am 5'9" tall
\' (Print single quote, so that the single quote does not signal the end of the string)
>>> print ('I am 5\'9" tall')
I am 5'9" tall
If you don’t want the characters to be preceded by the \ character to be interpreted as special characters, you may use raw strings and this can be used if you will add an r on the first quote. And if you don’t want \t to be interpreted as a tab, you must type print (r’Hi\tWorld’). You must get Hi \tWorld as the output.