5.4. String Immutable

  • str is immutable

  • str methods create a new modified str

>>> a = 'Python'
>>> a.replace('P', 'C')
'Cython'
>>> print(a)
Python
>>> a = 'Python'
>>> b = a.replace('P', 'C')
>>>
>>> print(a)
Python
>>> print(b)
Cython
>>> a = 'Python'
>>> a = a.replace('P', 'C')
>>>
>>> print(a)
Cython

5.4.1. Memory

../../_images/type-str-memory-1.png
../../_images/type-str-memory-2.png
../../_images/type-str-memory-3.png
../../_images/type-str-immutable.png

5.4.2. Value Check

This is valid way to check str value:

>>> name = 'Mark Watney'
>>> name == 'Mark Watney'
True

The following code will produce SyntaxWarning due to the invalid operand <input>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?:

>>> name = 'Mark Watney'
>>> name is 'Mark Watney'
False

5.4.3. Length

>>> len('hello')
5

5.4.4. Concatenation

  • Preferred string concatenation is using f-string formatting

>>> 'a' + 'b'
'ab'
>>> 'a' 'b'
'ab'
>>> data = 'one' \
...        'two' \
...        'three'
>>>
>>> data
'onetwothree'

5.4.5. Concat Problem

>>> 'Mark' + 'Watney'
'MarkWatney'
>>> 'Mark' + ' ' + 'Watney'
'Mark Watney'
>>> 'Mark Watney'
'Mark Watney'
>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> firstname + lastname
'MarkWatney'
>>>
>>> firstname + ' ' + lastname
'Mark Watney'
>>>
>>> f'{firstname} {lastname}'
'Mark Watney'

5.4.6. Concat Numbers

>>> 1 + 2
3
>>>
>>> '1' + '2'
'12'
>>>
>>> '1' + 2
Traceback (most recent call last):
TypeError: can only concatenate str (not "int") to str
>>>
>>> 1 + '2'
Traceback (most recent call last):
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> name = 'Mark Watney'
>>> age = 42
>>>
>>> 'Astronaut ' + name + ' is ' + age + ' years old.'
Traceback (most recent call last):
TypeError: can only concatenate str (not "int") to str
>>>
>>> 'Astronaut ' + name + ' is ' + str(age) + ' years old.'
'Astronaut Mark Watney is 42 years old.'
>>>
>>> f'Astronaut {name} is {age} years old.'
'Astronaut Mark Watney is 42 years old.'

5.4.7. Concat Multiply

>>> '*' * 10
'**********'
>>> text = 'Hello world'
>>> print(text + '\n' + '!'*len(text))
Hello world
!!!!!!!!!!!

5.4.8. Use Case - 0x01

>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> 'Hello ' + firstname + ' ' + lastname + '!'
'Hello Mark Watney!'
>>> firstname = 'Mark'
>>> lastname = 'Watney'
>>>
>>> f'Hello {firstname} {lastname}!'
'Hello Mark Watney!'