5.3. String Immutable
str
is immutablestr
methods create a new modifiedstr
>>> text = 'Alice'
>>> result = text.upper()
>>>
>>> print(result)
ALICE
>>>
>>> print(text)
Alice
5.3.1. Memory




5.3.2. Value Check
Use
==
to check if strings are equal
This is valid way to check str value:
>>> name = 'Alice'
>>>
>>> name == 'Alice'
True
5.3.3. Length
Builtin
len()
returns the length of a string
>>> len('Alice')
5
5.3.4. Concatenation
Preferred string concatenation is using
f-string
formatting
>>> 'alice' + '@' + 'example.com'
'alice@example.com'
>>> 'alice' '@' 'example.com'
'alice@example.com'
Line termination character \
is used to split long lines:
>>> result = 'alice' \
... '@' \
... 'example.com'
>>>
>>> result
'alice@example.com'
>>> data = (
... 'alice'
... '@'
... 'example.com'
... )
>>>
>>> data
'alice@example.com'
5.3.5. Concat Numbers
>>> name = 'Alice'
>>> age = 30
>>>
>>> 'User ' + name + ' has ' + str(age) + ' years'
'User Alice has 30 years'
>>> name = 'Alice'
>>> age = 30
>>>
>>> f'User {name} has {age} years'
'User Alice has 30 years'
5.3.6. Concat Multiply
>>> '*' * 10
'**********'