While you have already been writing strings, you still do not know what they do. In this exer- cise, we create a bunch of variables with complex strings so you can see what they are for.
First an explanation of strings.
A string is usually a bit of text you want to display to someone or “export” out of the program you are writing. Python knows you want something to be a string when you put either " (double- quotes) or ' (single-quotes) around the text. You saw this many times with your use of print when you put the text you want to go to the string inside " or ' after the print. Then Python prints it.
Strings may contain the format characters you have discovered so far. You simply put the format- ted variables in the string, and then a % (percent) character, followed by the variable. The only catch is that if you want multiple formats in your string to print multiple variables, you need to put them inside ( ) (parentheses) separated by , (commas). It’s as if you were telling me to buy you a list of items from the store and you said, “I want milk, eggs, bread, and soup.” Only as a programmer we say, “(milk, eggs, bread, soup).”
We will now type in a whole bunch of strings, variables, and formats, and print them. You will also practice using short abbreviated variable names. Programmers love saving themselves time at your expense by using annoying cryptic variable names, so let’s get you started being able to read and write them early on.
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print(x)
print(y)
print ("I said: %r." % x)
print ("I also said: '%s'." % y)
hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))
w = "This is the left side of..."
e = "a string with a right side."
print(w + e)
In [ ]:
There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.
What is the difference between %r and %s?
We use %r for debugging, since it displays the “raw” data of the variable, but we use %s and others for displaying to users.
What’s the point of %s and %d when you can just use %r?
The %r is best for debugging, and the other formats are for actually displaying variables to users.
If you thought the joke was funny could you write hilarious = True?
Yes, and you’ll learn more about these boolean values in Exercise 27.
Why do you put ' (single-quotes) around some strings and not others?
Mostly it’s because of style, but I’ll use a single-quote inside a string that has double-quotes. Look at line 10 to see how I’m doing that.
I get the error TypeError: not all arguments converted during string formatting. You need to make sure that the line of code is exactly the same. What happens in this error is you have more % format characters in the string than variables to put in them. Go back and figure out what you did wrong.