9.11.4. Strings and text#

9.11.4.1. Strings#

Besides numbers, Python can also manipulate strings, which can be expressed in several ways. A string is usually a bit of text you want to display to someone, or “export” out of the program you are writing. They can be enclosed in single quotes or double quotes:

1print('It works.')
2print('It doesn\'t matter.')
3print("It doesn't matter.")
4print('"Yes", he said.')
5print("\"Yes,\" he said.")
6print('"It\'s not", she said.')
It works.
It doesn't matter.
It doesn't matter.
"Yes", he said.
"Yes," he said.
"It's not", she said.

9.11.4.2. String formatting#

Additionally, Strings may contain format characters such as %d from the previous section to output or convert to integer decimals. Here are some more:

Conversion

Meaning

%d or %i

Signed integer decimal

%x or %X

Signed hexadecimal (lowercase/uppercase)

%e or %E

Floating point exponential (lowercase/uppercase)

%f or %F

Floating point decimal format

%c

Single character (accepts integer or single character string)

%r

String (converts any Python object using repr())

%s

String (converts any Python object using str())

Here are some examples to try for yourself:

 1x = "There are %d types of people." % 10
 2binary = "binary"
 3do_not = "don't"
 4y = "Those who know %s and those who %s." % (binary, do_not)
 5
 6print(x)
 7print(y)
 8
 9print("I said: %r." % x)
10print("I also said: '%s'." % y)
There are 10 types of people.
Those who know binary and those who don't.

Note

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer str.format() interface helps avoiding these errors. Check the official documentation for more on the topic.

9.11.4.3. More on strings#

String literals can span multiple lines in several ways. Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line:

1hello = "This is a rather long string containing\n\
2several lines of text just as you would do in C.\n\
3    Note that whitespace at the beginning of the line is \
4significant."
5
6print(hello)
This is a rather long string containing
several lines of text just as you would do in C.
Note that whitespace at the beginning of the line is significant.

Or, strings can be surrounded in a pair of matching triple-quotes: """ or '''. End of lines do not need to be escaped when using triple-quotes, but they will be included in the string. So the following uses one escape to avoid an unwanted initial blank line.

1print("""\
2Usage: thingy [OPTIONS]
3    -h                        Display this usage message
4    -H hostname               Hostname to connect to
5""")
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

If we make the string literal a “raw” string, n sequences are not converted to newlines, but the backslash at the end of the line, and the newline character in the source, are both included in the string as data. Thus, the example:

hello = r"This is a rather long string containing\n\
several lines of text much as you would do in C."

print(hello)
"This is a rather long string containing\n\several lines of text much as you would do in C."

9.11.4.4. Byte array….#

:)