*Memo:
- My post explains a string (1).
- My post explains a string (2).
- My post explains a string (3).
- My post explains str() (1).
- My post explains str() (2).
A string can be read by indexing as shown below:
*Memo:
- Indexing can be done with one or more
[index]in the range[The 1st index, The last index]:-
index(Required-Type:int):- Don't use
index=.
- Don't use
-
indexcan be signed indices(zero and positive and negative indices). - Error occurs if
indexis out of range. -
[slice][index]can be used.
-
v = 'ABCDEFGH'
print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
print(v[0:1][0], v[1:2][0], v[2:3][0], v[3:4][0],
v[4:5][0], v[5:6][0], v[6:7][0], v[7:8][0])
print(v[-8:-7][0], v[-7:-6][0], v[-6:-5][0], v[-5:-4][0],
v[-4:-3][0], v[-3:-2][0], v[-2:-1][0], v[-1:8][0])
# A B C D E F G H
A string can be read by slicing as shown below:
*Memo:
- Slicing can be done with one or more
[start:end:step]in the range[start, end):-
start(Optional-Default:None-Type:int/NoneType):- It's a start index(inclusive).
- If it's
None, it's the 1st index. - Don't use
start=.
-
end(Optional-Default:None-Type:int/NoneType):- It's an end index(exclusive).
- If it's
None, it's the string length. - Don't use
end=.
-
step(Optional-Default:None-Type:int/NoneType):- It's the interval of indices.
- If it's
None, it's1. - It cannot be zero.
- Don't use
end=.
- The
[]with at least one:is slicing. -
startandendcan be signed indices(zero and positive and negative indices). - Error doesn't occur even if
[start, end)is out of the range[The 1st index, The string length).
-
v = 'ABCDEFGH'
print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0:8:1])
print(v[-8:8:1])
print(v[-100:100:1])
# ABCDEFGH
print(v[::2])
# ACEG
print(v[::-2])
# HFDB
print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# CDEFGH
print(v[2::2])
print(v[-6::2])
# CEG
print(v[2::-2])
print(v[-6::-2])
# CA
print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# ABCDEF
print(v[:6:2])
print(v[:-2:2])
# ACE
print(v[:6:-2])
print(v[:-2:-2])
# H
print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# CDEF
print(v[2:6:2])
print(v[-6:-2:2])
# CE
print(v[2:6:-2])
print(v[-6:-2:-2])
# Nothing
v = 'ABCDEFGH'
print(v[-9], v[8])
# IndexError: string index out of range
A string cannot be changed by indexing, slicing and a del statement as shown below:
*Memo:
- A
delstatement cannot remove zero or more characters from a string by indexing and slicing but can remove one or more variables themselves.
v = 'abcdef'
v[1] = 'X'
v[-5] = 'X'
v[3:5] = ['Y', 'Z']
v[-3:-1] = ['Y', 'Z']
v[1], v[3:5] = 'X', ['Y', 'Z']
v[-5], v[-3:-1] = 'X', ['Y', 'Z']
# TypeError: 'str' object does not support item assignment
v = 'abcdef'
del v[1], v[3:5]
# del v[-5], v[-2:5]
# TypeError: 'str' object does not support item deletion
v = 'abcdef'
del v
print(v)
# NameError: name 'v' is not defined
If you really want to change a string, use list() and join() as shown below:
v = 'abcdef'
v = list(v)
v[1] = 'X'
v[-5] = 'X'
v[3:5] = ['Y', 'Z']
v[-3:-1] = ['Y', 'Z']
v[1], v[3:5] = 'X', ['Y', 'Z']
v[-5], v[-3:-1] = 'X', ['Y', 'Z']
v = ''.join(v)
print(v)
# aXcYZf
v = 'abcdef'
v = list(v)
del v[1], v[3:5]
# del v[-5], v[-2:5]
v = ''.join(v)
print(v)
# acd
A string can be continuously used through multiple variables as shown below:
v1 = v2 = v3 = 'ABCDE' # Equivalent
# v1 = 'ABCDE'
print(v1) # ABCDE # v2 = v1
print(v2) # ABCDE # v3 = v2
print(v3) # ABCDE
A string cannot be shallow-copied and deep-copied as shown below:
<Shallow & Deep copy>:
*Memo:
-
v1andv2refer to the same string and each same character. -
iscan check ifv1andv2refer to the same string and each same character. - copy.copy(), str() and slicing cannot shallow-copy a string.
- copy.deepcopy() cannot deep-copy and even shallow-copy a string.
import copy
v1 = 'ABCDE'
v2 = copy.copy(v1)
v2 = str(v1)
v2 = v1[:]
v2 = copy.deepcopy(v1)
print(v1, v1[2]) # ABCDE C
print(v2, v2[2]) # ABCDE C
print(v1 is v2, v1[2] is v2[2])
# True True
Top comments (0)