DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

String in Python (3)

Buy Me a Coffee

*Memo:

A string can be read by indexing and slicing as shown below:

*Memo:

  • Indexing can be done with one or more [index] in the range [The 1st element index, The last element index]:
    • index(Required-Type:int):
      • Don't use index=.
    • Error occurs if index is out of range.
  • Slicing can be done with one or more [start:end:step] in the range [start, end):
    • start(Optional-Default:None-Type:int and NoneType):
      • It's a start index(inclusive).
      • If it's None, it's the 1st element index.
      • Don't use start=.
    • end(Optional-Default:None-Type:int and NoneType):
      • It's an end index(exclusive).
      • If it's None, it's the next index to the last element index.
      • Don't use end=.
    • step(Optional-Default:None-Type:int and NoneType):
      • It's the interval of indices.
      • If it's None, it's 1.
      • It cannot be zero.
      • Don't use end=.
    • The [] with at least one : is slicing.
    • Error doesn't occur even if [start, end) is out of the range [The 1st element index, The last element index].
  • index, start and end can be signed indices(zero and positive and negative indices).
v = 'ABCDEFGH'

print(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])
# A B C D E F G H

print(v[:])
print(v[::])
print(v[None:None:None])
print(v[0: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
Enter fullscreen mode Exit fullscreen mode
v = 'ABCDEFGH'

print(v[-9], v[8])
# IndexError: string index out of range
Enter fullscreen mode Exit fullscreen mode

A string cannot be changed by indexing, slicing and a del statement as shown below:

*Memo:

  • A del statement 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
Enter fullscreen mode Exit fullscreen mode
v = 'abcdef'

del v[1], v[3:5]
# del v[-5], v[-2:5]
# TypeError: 'str' object does not support item deletion
Enter fullscreen mode Exit fullscreen mode
v = 'abcdef'

del v

print(v)
# NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
v = 'abcdef'

v = list(v)

del v[1], v[3:5]
# del v[-5], v[-2:5]

v = ''.join(v)

print(v)
# acd
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

A string cannot be shallow-copied and deep-copied as shown below:

<Shallow & Deep copy>:

*Memo:

  • v1 and v2 refer to the same string and each same character.
  • is keyword can check if v1 and v2 refer 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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)