DEV Community

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

Posted on • Edited on

String in Python (1)

Buy Me a Coffee

*Memo:

A string(str):

  • is the ordered immutable(hashable) collection of zero or more characters whose type is str and which allows duplicated characters:
    • Ordered means that the order of each character in a string is kept so it guarantees that the order is always the same.
    • Immutable(Hashable) means the characters of a string cannot be changed.
  • can be used with len() to get the length.
  • is False if it's empty.
  • can be checked if a specific character is in the string with in keyword.
  • can be checked if the string is referred to by two variables with is keyword.
  • can be enlarged with * and a number.
  • can be iterated with a for statement.
  • can be created by the string literal '', "", '''''' or """""" or str() with or without any types of values:
    • A string literal can have a lot of Unicode characters.
    • A string literal can also be used for a docstring.
    • '' or "" is for one line.
    • '''''' or """""" is for one or more lines.
    • For str(), the words type conversion are also suitable in addition to the word creation.
  • can be decoded to from a bytes or bytearray by decode():
    • For decode(), the words creation and type conversion are also suitable in addition to the word encoding.
  • cannot be big because it gets OverflowError.
  • can be read by indexing and slicing.
  • cannot be changed by indexing, slicing and a del statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • can be continuously used through multiple variables.
  • cannot be shallow-copied by copy.copy(), str() and slicing.
  • cannot be deep-copied and even shallow-copied by copy.deepcopy().

Be careful, a big string gets OverflowError.


'', "", '''''' or """""" can create a string as shown below:

*Memo:

  • \' is the escape sequence to output '.
v = '' # Empty str
v = "Hello World"
v = "Lёт's gφ!" # Let's go!
v = "I'm John."
v = 'I\'m John.'
v = '''I'm John.'''
v = """I'm John."""
v = '''Apple Orange Banana Kiwi'''
v = 'Apple' " Orange" '''Banana''' """Kiwi"""
v = '''Apple
Orange
Banana
Kiwi'''
v = """
Apple
   Orange
       Banana
           Kiwi
"""
'These above get no error'
"These above get no error"
'''These above get no error'''
"""These above get no error"""
''' 
These above 
get no error 
'''
"""
These above 
get no error 
"""
# No error

print(len("Let's go!"))
for v in 'ABC': pass
v = 'ABCDE' * 3
v = '01234' * 3
v = '' * 3
v1, v2, v3 = 'ABC'
v1, *v2, v3 = 'ABCDEF'
for v1, v2, v3 in ['ABC', 'DEF']: pass
for v1, *v2, v3 in ['ABCDEF', 'GHIJKL']: pass
print(*'ABCD', *'ABCD')
print([*'ABCD', *'ABCD'])
# No error

v = 'ABCDE' * 1000000000
# Error
Enter fullscreen mode Exit fullscreen mode

A string is the ordered immutable(hashable) collection of zero or more characters whose type is str and which allows duplicated characters as shown below:

v = "Hello World"

print(v)
# Hello World

print(type(v))
# <class 'str'>

v[1] = 'X'
v[3] = 'Y'
# TypeError: 'str' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
v = '' # Empty str

print(v)
# Nothing
Enter fullscreen mode Exit fullscreen mode
v = "Lёт's gφ!" # Let's go!

print(v)
# Lёт's gφ!
Enter fullscreen mode Exit fullscreen mode
v = "I'm John."
v = 'I\'m John.'
v = '''I'm John.'''
v = """I'm John."""

print(v)
# I'm John.
Enter fullscreen mode Exit fullscreen mode
v = '''Apple Orange Banana Kiwi'''
v = 'Apple' " Orange" ''' Banana''' """ Kiwi"""

print(v)
# Apple Orange Banana Kiwi
Enter fullscreen mode Exit fullscreen mode
v = '''Apple
Orange
Banana
Kiwi'''

print(v)
# Apple
# Orange
# Banana
# Kiwi
Enter fullscreen mode Exit fullscreen mode
v = """
Apple
   Orange
       Banana
           Kiwi
"""

print(v)
# 
# Apple
#    Orange
#        Banana
#            Kiwi
#
Enter fullscreen mode Exit fullscreen mode

A string can be used with len() to get the length as shown below:

v = "Let's go!"

print(len(v))
# 9
Enter fullscreen mode Exit fullscreen mode

An empty string is False as shown below:

print(bool('')) # Empty str
# False

print(bool(' ')) # str
# True
Enter fullscreen mode Exit fullscreen mode

A string can be checked if a specific character is in the string with in keyword as shown below:

v = 'ABCD'

print('B' in v)
# True

print('CD' in v)
# True

print('b' in v)
# False
Enter fullscreen mode Exit fullscreen mode

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 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

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)