1.8. Strings Part II

1.8.1. Triple Quoted String Literals

Strings delimited by one quote character, like ', are required to lie within a single Python line. It is sometimes convenient to have a multi-line string, which can be delimited with triple quotes, '''. Try typing the following. You will get continuation lines until the closing triple quotes. Try in the Shell:

sillyTest = '''Say,
"I'm in!"
This is line 3'''
print(sillyTest)

The line structure is preserved in a multi-line string. As you can see, this also allows you to embed both single and double quote characters!

1.8.2. Escape Codes

Continuing in the Shell with sillyTest, enter just

sillyTest

The answer looks strange! It indicates an alternate way to encode the string internally in Python using escape codes. Escape codes are embedded inside string literals and start with a backslash character \. They are used to embed characters that are either unprintable or have a special syntactic meaning to Python that you want to suppress. In this example you see the most common ones:

Escape code Meaning
\\ \ (backslash)
\n newline
\' ' (single quote)

The newline character indicates further text will appear on a new line when printed. When you use a print function, you get the actual printed meaning of the escape coded character.

Predict the result, and try in the Shell:

print('a\nb\n\nc')

Did you guess the right number of lines splitting in the right places?

Table Of Contents

Previous topic

1.7. Print Function, Part I

Next topic

1.9. The Idle Editor and Execution

This Page