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 enlarged with * and a number as shown below:

v = 'ABCDE' * 3

print(v)
# ABCDEABCDEABCDE
Enter fullscreen mode Exit fullscreen mode
v = '01234' * 3

print(v)
# 012340123401234
Enter fullscreen mode Exit fullscreen mode
v = '' * 3

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

A string and other strings can be concatenated with + as shown below:

v = 'ABC' + 'DE' + 'FGHI'

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

A string and other strings cannot return:

  • all the characters in them with '|' (Union: A ∪ B).
  • their common characters with '&' (Intersection: A ∩ B).
  • the characters in the string which aren't in other string with '-' (Difference: A - B).
print('AE' | 'ACE' | 'ABDE')
# TypeError: unsupported operand type(s) for |: 'str' and 'str'
Enter fullscreen mode Exit fullscreen mode
print('AE' & 'ACE' & 'ABDE')
# TypeError: unsupported operand type(s) for &: 'str' and 'str'
Enter fullscreen mode Exit fullscreen mode
print('AE' - 'ACE' - 'ABDE')
# TypeError: unsupported operand type(s) for -: 'str' and 'str'
Enter fullscreen mode Exit fullscreen mode

A string and other string cannot return the characters in the string but not in other string or not in the string but in other string with '^' (Symmetric Difference: A Δ B) as shown below:

print('ABCD' ^ 'ACE')
# TypeError: unsupported operand type(s) for ^: 'str' and 'str'
Enter fullscreen mode Exit fullscreen mode

A string can be iterated with a for statement as shown below:

for v in 'ABC':
    print(v)
# A
# B
# C
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

Be careful, a big string gets OverflowError as shown below:

v = 'ABCDE' * 1000000000

print(v)
# OverflowError: repeated string is too long
Enter fullscreen mode Exit fullscreen mode

Top comments (0)