Skip to main content

5.7) Manipulating Strings


As with numbers, you can do operations on strings.

Let's see what happens if you add the string 'Hi' with 'There':

>>> greeting='Hi'+'There'
>>> print greeting
HiThere

This is what we call string concatenation. It’s like sticking strings together one after the other.

You can also multiply a string with an integer to repeat the string a number of times.

>>> print 'He'*5
HeHeHeHeHe

Note that although you can mix integers and floats in operations, you can’t mix numbers and strings:

>>> '1' + 4
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    '1'+3
TypeError: cannot concatenate 'str' and 'int' objects