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 True if it's non-empty and False if it's empty, checking it with bool().
  • is False if it's non-empty and True if it's empty, inverting the truth value with not keyword.
  • can be checked if specific characters are and aren't in the string with in keyword and with not and in keyword respectively.
  • can be checked if the string is and isn't referred to by two variables with is keyword and with is and not keyword respectively.
    • Be careful, string literals with is keyword and with is and not keyword get warnings so use == and != respectively.
  • and other string can be checked if all the characters in:
    • them are and aren't the same with == and != respectively.
    • the string are in other string with <=.
    • other string are in the string with >=.
    • the string and other characters are in other string with <.
    • other string and other characters are in the string with >.
  • and other string cannot be checked if they have and don't have their common characters with bool() and & and with not keyword and & respectively.
  • can be enlarged with * and a number.
  • and other strings can be concatenated with +.
  • and other string 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).
    • the characters in either the string or other string but not both with '^' (Symmetric Difference: A Δ B).
  • can be iterated with a for statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • can be created by the string literal '', "", '''''' and """""" and by 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 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!"))
print(bool('0'))
print(bool(' '))
print(bool(''))
print(not '0')
print(not ' ')
print(not '')
print('A' in 'ABC')
print('A' not in 'ABC')
print('ABC' is 'ABC')     # It gets warning so use `==`.
print('ABC' is not 'ABC') # It gets warning so use `!=`.
print('ABC' == 'ABC')
print('ABC' != 'ABC')
print('ABC' <= 'ABC')
print('ABC' >= 'ABC')
print('ABC' < 'ABC')
print('ABC' > 'ABC')
print('ABCDE' * 3)
print('01234' * 3)
print('' * 3)
print('ABC' + 'DE' + 'FGHI')
for v in 'ABC': print(v)
v1, v2, v3 = 'ABC'; print(v1, v2, v3)
v1, *v2, v3 = 'ABCDEF'; print(v1, v2, v3)
for v1, v2, v3 in ['ABC', 'DEF']: print(v1, v2, v3)
for v1, *v2, v3 in ['ABCDEF', 'GHIJKL']: print(v1, v2, v3)
print(*'ABCD', *'ABCD')
print([*'ABCD', *'ABCD'])
# No error

print(bool('ABC' & 'BD'))
print(not ('ABC' & 'BD'))
print('AE' | 'ACE')
print('ABCD' & 'ACE')
print('ABCD' - 'ACE')
print('ABCD' ^ 'ACE')
print('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

Top comments (0)