Exercise 6. Strings and Text

While you have been writing strings, you still do not know what they do. In this exercise 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 inside the string inside " or ' after the print to print the string.

Strings can contain any number of variables that are in your Python script. Remember that a variable is any line of code where you set a name = (equal) to a value. In the code for this exercise, types_of_people = 10 creates a variable named types_of_people and sets it = (equal) to 10. You can put that in any string with {types_of_people}. You also see that I have to use a special type of string to “format”; it’s called an “f-string” and looks like this:

f"some stuff here {avariable}"
f"some other stuff {anothervar}"

Python also has another kind of formatting using the .format() syntax, which you see on line 17. You’ll see me use that sometimes when I want to apply a format to an already-created string, such as in a loop. We’ll cover that more later.

We will now type in a whole bunch of strings, variables, and formats, and print them. We will also practice using short, abbreviated variable names. Programmers love saving time at your expense by using annoyingly short and cryptic variable names, so let’s get you started reading and writing them early on.

ex6.py


 1    types_of_people = 10
 2    x = f"There are {types_of_people} types of people."
 3
 4    binary = "binary"
 5    do_not = "don't"
 6    y = f"Those who know {binary} and those who {do_not}."
 7
 8    print(x)
 9    print(y)
10
11    print(f"I said: {x}")
12    print(f"I also said: '{y}'")
13
14    hilarious = False
15    joke_evaluation = "Isn't that joke so funny?! {}"
16
17    print(joke_evaluation.format(hilarious))
18
19    w = "This is the left side of..."
20    e = "a string with a right side."
21
22    print(w + e)

What You Should See

Exercise 6 Session


$ python3.6 ex6.py
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.

Study Drills

1. Go through this program and write a comment above each line explaining it.

2. Find all the places where a string is put inside a string. There are four places.

3. Are you sure there are only four places? How do you know? Maybe I like lying.

4. Explain why adding the two strings w and e with + makes a longer string.

Break It

You are now at a point where you can try to break your code to see what happens. Think of this as a game to devise the most clever way to break the code. You can also find the simplest way to break it. Once you break the code, you then need to fix it. If you have a friend, then the two of you can try to break each other’s code and fix it. Give your friend your ex6.py file so they can break something. Then you try to find their error and fix it. Have fun with this, and remember that if you wrote this code once you can do it again. If you take your damage too far, you can always type it in again for extra practice.

Common Student Questions

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 lines 6 and 15 to see how I’m doing that.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.240.21