Chapter 6 escape characters Flashcards
What are escape characters?
Escape characters represent characters in string values that would otherwise be
difficult or impossible to type into code.
What do the \n and \t escape characters represent?
\n is a newline; \t is a tab.
How can you put a \ backslash character in a string?
The \ escape character will represent a backslash character.
The string value “Howl’s Moving Castle” is a valid string. Why isn’t it a problem that the single quote
character in the word Howl’s isn’t escaped?
The single quote in Howl’s is fine because you’ve used double quotes to mark the
beginning and end of the string.
If you don’t want to put \n in your string, how can you write a string with newlines in it?
Multiline strings allow you to use newlines in strings without the \n escape
character.
What do the following expressions evaluate to?
‘Hello world!’[1] ‘Hello world!’[0:5] ‘Hello world!’[:5] ‘Hello world!’[3:]
The expressions evaluate to the following:
‘e’ ‘Hello’ ‘Hello’ ‘lo world!
What do the following expressions evaluate to?
‘Hello’.upper() ‘Hello’.upper().isupper() ‘Hello’.upper().lower()
The expressions evaluate to the following:
‘HELLO’ True ‘hello’
What do the following expressions evaluate to?
‘Remember, remember, the fifth of November.’.split() ‘-‘.join(‘There can be only one.’.split())
The expressions evaluate to the following:
[‘Remember,’, ‘remember,’, ‘the’, ‘fifth’, ‘of’, ‘November.’] ‘There-can-be-only-one.’
What string methods can you use to right-justify, left-justify, and center a string?
The rjust(), ljust(), and center() string methods, respectively
How can you trim whitespace characters from the beginning or end of a string?
The lstrip() and rstrip() methods remove whitespace from the left and right ends of a string, respectively.