DEV Community

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

Posted on

String in Python (2)

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].
  • Slicing can be done with one or more [start:end:step]:
    • start(Optional-Default:The index of the 1st element):
      • It's a start index(inclusive).
    • end(Optional-Default:The index of the last element + 1):
      • It's an end index(exclusive).
    • step(Optional-Default:1):
      • It's the interval of indices.
      • It cannot be zero.
    • The [] with at least one : is slicing.
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[::])
# 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

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[3:5] = ['Y', 'Z']
# TypeError: 'str' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
v = 'abcdef'

del v[1], v[3: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[3:5] = ['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]

v = ''.join(v)

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

A string can be unpacked with an assignment and for statement, the function and * but not with ** as shown below:

v1, v2, v3 = 'ABC'

print(v1, v2, v3)
# A B C
Enter fullscreen mode Exit fullscreen mode
v1, *v2, v3 = 'ABCDEF'

print(v1, v2, v3)    # A ['B', 'C', 'D', 'E'] F
print(v1, *v2, v3)   # A B C D E F
print(*v1, *v2, *v3) # A B C D E F
Enter fullscreen mode Exit fullscreen mode
for v1, v2, v3 in ['ABC', 'DEF']:
    print(v1, v2, v3)
# A B C
# D E F
Enter fullscreen mode Exit fullscreen mode
for v1, *v2, v3 in ['ABCDEF', 'GHIJKL']:
    print(v1, v2, v3)
    print(v1, *v2, v3)
    print(*v1, *v2, *v3)
# A ['B', 'C', 'D', 'E'] F
# A B C D E F
# A B C D E F
# G ['H', 'I', 'J', 'K'] L
# G H I J K L
# G H I J K L
Enter fullscreen mode Exit fullscreen mode
print(*'ABCD', *'EF')
# A B C D E F
Enter fullscreen mode Exit fullscreen mode
print([*'ABCD', *'EF'])
# ['A', 'B', 'C', 'D', 'E', 'F']
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*'ABCD', *'EF')
# A B C D E F
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, [0, 1, *args, 2, 3])

func()
# a b ()
# a b Nothing
# a b [0, 1, 2, 3]

func(*'ABCD', *'EF')
# A B ('C', 'D', 'E', 'F')
# A B C D E F
# A B [0, 1, 'C', 'D', 'E', 'F', 2, 3]
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/or 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)