3.4.2. Strings

We have already been introduced to strings as a basic data type. Now we take a look them again from a different point of view. Strings are containers. This means you can look at their insides and do things like check whether the first character is capitalized and whether the third character is “e”. Indexing strings, string slices

To get at the inner components of strings Python uses the same syntax and operators as lists. The Pythonic conception is that both lists and strings belong to a ‘super’ data type, sequences. Sequence types are containers that contain elements in a particular order, so indexing by number makes sense for all sequences:

>>> X = 'dogs'
>>> X[0]
>>> X[1]
>>> X[-1]

The following raises an IndexError, as it would with a 4-element list:

>>> X[4]
IndexError: string index out of range

Strings can also be one element long:

>>> Y = 'd'


Unlike C, there is no special type for characters in Python. Characters are just one-element strings.

And they can be empty, just as lists can:

>>> Z = ''

As with lists, you can check the contents of strings. So:

>>> 'd' in X
>>> 'do' in X
>>> 'dg' in X

Python also provides easy access to subsequences of a string, just as it does for lists. The following examples illustrate how to make such references:

>>> X[0:2] # string of 1st and 2nd characters
>>> X[:-1] # string excluding last character

References to subsequences of a string are called slices.

Guido va Rossum says: “The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n”:

 | H | e | l | p | A |
 0   1   2   3   4   5
-5  -4  -3  -2  -1

The first row of numbers gives the position of the indices 0…5 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.

For nonnegative indices, the length of a slice is the difference of the indices, if both are within bounds, e.g., the length of word[1:3] is 2.

The built-in function len() returns the length of a string:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)

Strings can also be concatenated into longer sequences, just as lists can:

>>> X + Y

Using the name of the type as a function gives us a way of MAKING strings, just as it did with lists:

>>> One = str(1)

One is no longer an int!:

>>> One
>>> I = int(str(1))
>>> I

And as with lists, calling the type with no arguments produces the empty string:

>>> Empty = str()
>>> Empty

There is one thing that can be done with lists that canNOT be done with strings. Assignment of values:

>>> 'spin'[2]= 'a'
TypeError: object does not support item assignment

This can be fixed, by avoiding the assignment or making it on a mutable sequenece, such as a list, which contains the relevant information.

See also

Section Mutability (advanced). String methods

In all the following examples, S is a string. This is just a sample. See the official Python docs for the complete list of string methods. Or just type help(str) at the Python prompt!


Return a string just like S, except that it is capitalized. If S is already capitalized, the result is identical to S.


Return the number of times x appears in the string S.


Return the index in L of the first substring whose identicql to x. It is an error if there is no such item.


Return index of first instance of t in S, or -1 if not found


Return index of last instance of t in S, or -1 if not found


Combine the strings of Seq into single string using S as the glue. ‘ ‘.join([“See”,”John”,”run”]) produces:

"See John run"


Return a string in which every instance of the substring x in L is replaced with y:

>>> X = 'abracadabra'
>>> X.replace('dab','bad')
>>> X.replace('a','b')


Split S into a list wherever a t is found. If t is not supplied, split wherever a space is found.


Split S into a list of strings, one per line.


Copy of S without leading or trailing whitespace.


Return a string just like S in which all words are capitalized:

>>> 'los anGeles'.title()
'Los Angeles'


Return True is every word in S is capitalized. Otherwise, return False:

>>> 'los anGeles'.istitle()
>>> 'Los AnGeles'.istitle()
>>> 'Los Angeles'.istitle()

Reverse the elements of the list, in place.