Python Strings

In Python, a string is a data type that's typically used to represent text. A string could be any series of characters, including letters, numbers, spaces, etc.

In most languages (Python included), a string must be enclosed in either single quotes (') or double quotes (") when assigning it to a variable.

All of the following lines are strings being assigned to a variable:

So if we print those out it would look like this:

Hey there!
742 Evergreen Terrace
'How long is a piece of string?' he asked
'!$*#@ you!' she replied

Return a String's Length

You can use the len() function to return a string's length. To do this, simply pass the string in as an argument to the function.


Numbers in Strings

If you need to store numbers that may have calculations performed on them, don't make them a string. There's a difference between the following two declarations:

In the first line, the value 1 is a number. In the second line, it's a string.

Including numbers in a string simply makes them part of that string. So you can't do things like add two numbers together if they're actually a string. Arithmetic operators have a different purpose when used on strings. For example, when working with numbers, the plus sign (+) adds two numbers together, but when working with strings, it concatenates the strings together.

So doing this:

Results in this:


The first line is a result of adding two numbers. The second line is a result of concatenating two strings.

Detecting Numbers in Strings

Python includes functions that allow you to detect whether a string consists solely of numbers. More specifically, it includes the isnumeric(), isdigit(), and isdecimal() functions. These allow you to be specific about the type of number the string contains.

These functions return true if all characters in the string match (and there's at least one character). Otherwise they return false.

Here's an example of using isnumeric():


The isnumeric() function is broad and it includes all results that would be returned by isdigit() and isdecimal().

Quotes in Strings

You'll get an error if you do this:

You'll get an error because the string is enclosed in double quotes, but the actual string itself also contains double quotes. The same issue will apply if you use single quotes for both purposes.

In this case you can use the backslash (\) to escape the quote characters within the string.

In Python, the backslash is used to escape characters that otherwise have a special meaning, such as the quote character, newline, tab, and even the backslash itself.

So we can change the above code to this:

Once again he asked "How long is a piece of string?"

Another way to deal with this issue is to use double quotes to enclose the string, but single quotes within the string (or vice-versa).

Strings that Span Multiple Lines

There are a couple of different ways to make a string span multiple lines.

Escape Sequence

Just as we used the backslash to escape the double quotes, we can use an escape sequence to force a string to span multiple lines.

For those about to rock, we salute you!

Triple Quotes

You can also use triple quotes to enclose strings that span multiple lines. This is especially handy if you have many lines, as it saves you from having to put a backslash on the end of every line.

So you can use triple double quotes:

Or triple single quotes

And the result is this:

This string
multiple lines

Escape Sequences

Here's a table of the escape sequences available in Python:

Escape Sequence Description
\newline Backslash and newline ignored
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT)
\ooo Character with octal value ooo
\xhh Character with hex value hh

Some escape sequences are only recognized in string literals. These are:

Escape Sequence Description
\N{name} Character named name in the Unicode database
\uxxxx Character with 16-bit hex value xxxx. Exactly four hexadecimal digits are required.
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx. Exactly eight hexadecimal digits are required.

How to Access Part of a String

You can access a single character from your string like this:


The reason the letter p was returned is because we specified 2 as the index inside square brackets. Python uses zero-based indexing (i.e. it starts counting at zero — like 0, 1, 2, 3 etc), so this results in the third letter being printed.

You can retrieve a range of characters by using two digits separated by a colon. Like this:


String Formatting Operator

The % symbol has special meaning in Python strings. It can be used as a placeholder for another value to be inserted into the string. The % symbol is a prefix to another character which defines the type of value to be inserted.

It works like this:

Hello Homer, you scored 3 out of 100

So we used %s where we wanted to insert a string and %i for an integer. The values are provided after the % after the end of the string.