Operators and functions

The operators and functions that work with character strings return new character strings derived from the original. The operators (in, + and *) and built-in functions (len, max and min) work with character strings in the same way as with lists and tuples.

>>> welcome = "Hello pythonistas!\n"
>>> 2 * welcome
'Hello pythonistas!\nHello pythonistas!\n'
>>> welcome + welcome
'Hello pythonistas!\nHello pythonistas!\n'
>>> "python" in welcome
True
>>> max(welcome)
'y'
>>> min(welcome)
'\n'

Indexing and slicing

The index and slice notation works in the same way to obtain individual elements or slices:

>>> welcome[0:5]
'Hello'
>>> welcome[6:-1]
'pythonistas!'

However, the index and slice notation cannot be used to add, remove or replace elements, as character strings are immutable:

>>> welcome[6:-1] = "everybody!"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Conversions

Converting character strings into numbers

You can use the int and float functions to convert character strings into integer or floating point numbers. If a character string is passed that cannot be interpreted as a number of the specified type, these functions trigger a ValueError exception. Exceptions are explained in more detail in control flow. You can also pass int an optional second parameter that specifies the numerical base to be used when interpreting the string:

 1>>> float("12.34")
 212.34
 3>>> float("12e3")
 412000.0
 5>>> int("1000")
 61000
 7>>> int("1000", base=10)
 81000
 9>>> int("1000", 8)
10512
11>>> int("1000", 2)
128
13>>> int("1234", 2)
14Traceback (most recent call last):
15  File "<stdin>", line 1, in <module>
16ValueError: invalid literal for int() with base 2: '1234'
Lines 5–8

If no second parameter is specified, int calculates with a base of 10.

Lines 9, 10

1000 is interpreted as an octal number.

Lines 11, 12

1000 is interpreted as a binary number.

Lines 13–16

1234 cannot be specified as an integer on base 2. A ValueError exception is therefore triggered.

Changing character strings with list manipulations

Since str objects are immutable, there is no way to change them directly like lists. However, you can convert them into lists:

>>> palindromes = "lol level gag"
>>> palindromes_list = list(palindromes)
>>> palindromes_list.reverse()
>>> "".join(palindromes_list)
'gag level lol'

Converting objects into strings

In Python, almost anything can be converted into a string using the built-in str function:

>>> data_types = [(7, "Data types", 19), (7.1, "Numbers", 19), (7.2, "Lists", 23)]
>>> (
...     "The title of chapter "
...     + str(data_types[0][0])
...     + " is «"
...     + data_types[0][1]
...     + "»."
... )
'The title of chapter 7 is «Data types».'

The example uses str to convert an integer from the data_types list into a string, which is then concatenated again to form the final string.

Note

While str is mostly used to generate human readable text, repr() is more commonly used for debugging output or status reports, for example to get information about the built-in Python function len():

>>> repr(len)
'<built-in function len>'

Checks

  • For example, can you add or multiply a string with an integer, a floating point number or a complex number?

  • Which of the following strings cannot be converted into numbers and why?

  • int("1e2")

  • int(1e+2)

  • int("1+2")

  • int("+2")