In Exercise 9 I threw you some new stuff, just to keep you on your toes. I showed you two ways to make a string that goes across multiple lines. In the first way, I put the characters \n (backslash n) between the names of the months. These two characters put a new line character into the string at that point.
This \ (backslash) character encodes difficult-to-type characters into a string. There are various “escape sequences” available for different characters you might want to use. We’ll try a few of these sequences so you can see what I mean.
An important escape sequence is to escape a single-quote ' or double-quote ". Imagine you have a string that uses double-quotes and you want to put a double-quote inside the string. If you write "I "understand" joe." then Python will get confused because it will think the " around "understand" actually ends the string. You need a way to tell Python that the " inside the string isn’t a real double-quote.
To solve this problem you escape double-quotes and single-quotes so Python knows to include in the string. Here’s an example:
"I am 6'2\" tall."
'I am 6\'2" tall.'
The second way is by using triple-quotes, which is just """ and works like a string, but you also can put as many lines of text as you want until you type """ again. We’ll also play with these.
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Catnip\n\t* Grass
What You Should See
Look for the tab characters that you made. In this exercise the spacing is important to get right.
$ python ex10.py
I'm tabbed in.
on a line.
I'm \ a \ cat.
I'll do a list:
* Cat food
This is all of the escape sequences Python supports. You may not use many of these, but memorize their format and what they do anyway. Try them out in some strings to see if you can make them work.
||What it does.
||ASCII bell (BEL)
||ASCII backspace (BS)
||ASCII formfeed (FF)
||ASCII linefeed (LF)
||Character named name in the Unicode database (Unicode only)
||Carriage Return (CR)
||Horizontal Tab (TAB)
||Character with 16-bit hex value xxxx (u” string only)
||Character with 32-bit hex value xxxxxxxx (u” string only)
||ASCII vertical tab (VT)
||Character with octal value ooo
||Character with hex value hh
If you use \U or \u then you’ll need to use a unicode string in u'\U0001F47E'. Put a u in front of the '' (single-quotes) or "" (double-quotes).
Here’s a tiny piece of fun code to try out:
for i in ["/","-","|","\\","|"]:
print "%s\r" % i,
- Memorize all the escape sequences by putting them on flash cards.
- Use ''' (triple-single-quote) instead. Can you see why you might use that instead of """?
- Combine escape sequences and format strings to create a more complex format.
- Remember the %r format? Combine %r with double-quote and single-quote escapes and print them out. Compare %r with %s. Notice how %r prints it the way you’d write it in your file, but %s prints it the way you’d like to see it?
Common Student Questions
- What if I wanted to start the months on a new line?
You simply start the string with \n like this:
- I still haven’t completely figured out the last exercise. Should I continue?
- Yes, keep going. Instead of stopping, take notes listing things you don’t understand for each exercise. Periodically go through your notes and see if you can figure these things out after you’ve completed more exercises. Sometimes though you may need to go back a few exercises and do them again.
- What makes \\ special compared to the other ones?
- It’s simply the way you would write out one backslash (\) character. Think about why you would need this.
- When I write // or /n it doesn’t work.
- That’s because you are using a forward-slash / and not a backslash \. They are different characters that do very different things.
- When I use a %r format none of the escape sequences work.
- That’s because %r is printing out the raw representation of what you typed, which is going to include the original escape sequences. Use %s instead. Always remember this: %r is for debugging, %s is for displaying.
- I don’t get Study Drill 3. What do you mean by “combine” escape sequences and formats?
- One concept I need you to understand is that each of these exercises can be combined to solve problems. Take what you know about format strings and write some new code that uses format strings and the escape sequences from this exercise.
- What’s better, ''' or """?
- It’s entirely based on style. Go with the ''' (triple-single-quote) style for now but be ready to use either depending on what feels best or what everyone else is doing.