title: The Python Strings
str objects, or strings, to be expressed in a few different ways:
- Single quotes:
`'Single quote strings can have "double" quotes inside.'`
- Double quotes:
`"Double quote strings can have 'single' quotes inside."`
- Triple quoted:
"""Triple quoted strings can span multiple lines. Unescaped "double" and 'single' quotes in triple quoted strings are retained.""" '''Triple quoted strings can be 'single'or "double" quotes. Unescaped newlines are also retained.'''
- Immutable: You cannot directly edit/change a Python string after you have created it. For example, if you try to directly reassign/change the first letter in a string, an error is thrown.
>>> foo = "my string" >>> foo = "a" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignmentInstead, you can convert the string into a list, modify the list element (string character) you wish to change, and then join the list elements back to a string, like so:
>>> foo = "my string" >>> foo_list_form = list(foo) >>> foo_list_form = "a" >>> foo = ' '.join(foo_list_form) >>> print(foo) ay string # The required output
- Indexable: You can access any character of
strobject by specifying its index. And as it supports slicing like in
>>> foo = "my string" >>> foo 's' >>> foo[3:] 'string' >>> foo[::-1] 'gnirts ym'
If we have a string
S, we can access its length with the command
>>> S = 'Hero' >>> len(S) 4
Accessing elements, Indexing and Slicing:
Strings are zero-indexed. We can associate each element in a string with a number, the
1st element indexed at
0. So, a string of
N characters would have
N indices, from
Indices help us to access elements of the string.
>>> S = 'ABCDEFGH' >>> S 'A' >>> S 'C' >>> S Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> S IndexError: string index out of range >>> >>> S 'H'
As we see above, we type
S to access the 1st element of the string, i.e.,
'A'. Also note that since the indices start at
0, the last element in the string
ABCDEFGH has the index
7 and not
8, even though the string itself has length
8. Hence, we see that
IndexError message when we try to access
Another cool thing we can do with strings is slicing. Instead of accessing individual elements, we can access chunks of the string with slices. The syntax is
start (default value
0) refers to the index at which we want to begin slicing,
stop (default value
len(S)) is the index where we want to end slicing, and
step (default value
1) is the jump after each element. Let’s look at this in action.
>>> S = 'ABCDEFGH' >>> S[0:5:1] 'ABCDE' >>> S[2:8] 'CDEFGH' >>> S[1::2] 'BDFH' >>> S[::-1] 'HGFEDCBA'
Please note that the part of string that gets printed out goes up to
stop - 1 value and not
S[0:5:1] starts at
S and stops at
When we don’t specify a value for a parameter, Python automatically takes the default value as stated above.
See how making the
step equal to
-1 just reverses the string. Cool, huh?
Feel free to experiment with different combinations to get comfortable with slicing. It’s going to be very useful.
Concatenation and Repetition:
>>> 2 + 3 5 >>> 'My' + ' ' + 'Hero' + ' ' + 'Academia' 'My Hero Academia' >>> >>> 4 * 9 36 >>> 'Yo!' * 3 'Yo!Yo!Yo!'
* operators are said to be
overloaded because they behave differently for different types of objects.
+ operator on strings leads to
concatenation, while the
* operator results in