Triple quotes
To display a long message, we can use the triple quote symbol (''' or """) to span a message over multiple lines.
Example
print('''Hello World.
My name is Bee and I am -- years old''')
Output
Hello World.
My name is Bee and I am -- years old
This helps with readability.
Escape characters
We can print special 'unprintable' characters such as a tab, quote or a newline. In this case, we use the backslash (\) to escape characters that otherwise have a different meaning.
Example
To print a tab, we type a backslash before the letter t (\t). Without it, the t will be printed. With it, a tab is printed. So, if we type print("Hello\tWorld") we will get
Hello World
Other uses
\n prints a newline.
print("Hello\nWorld")
Output:
Hello
World
\\ prints the backslash itself.
print("\\")
Output:
\
\' prints a single quote to prevent ending a string in single quotes.
print('You\'re a dingus')
Output:
You're a dingus
\" prints a double quote to prevent ending a string in double quotes.
print("I'm not a \"dingus\"")
Output:
I'm not a "dingus"
Raw strings
To print characters unpreceded by a backslash to be interpreted as special characters, we can use raw strings by adding a small r before the first quote of a string.
Example
For \t to not be interpreted as a tab, we will type print(r"dingus\tshabingus").
Output
dingus\tshabingus
No comments:
Post a Comment