*Memo:
- My post explains a string (2).
- My post explains a string (3).
- My post explains a string (4).
- My post explains str() (1).
- My post explains str() (2).
- My post explains a bytes (1).
- My post explains a bytearray (1).
- My post explains string, bytes and bytearray functions.
- My post explains f-strings.
- My post explains format().
- My post explains format_map().
- My post explains 10 collection types and their related posts.
A string(str):
- is the ordered immutable(hashable) collection of zero or more characters whose type is
strand 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
Trueif it's non-empty andFalseif it's empty, checking it with bool(). - is
Falseif it's non-empty andTrueif it's empty, inverting the truth value withnot. - can be checked if specific characters are and aren't in the string with
inandnot inrespectively. - can be checked if the string is and isn't referred to by two variables with
isandis notrespectively.- Be careful, string literals with
isandis notget warnings so use==and!=respectively.
- Be careful, string literals with
- and other string can be checked if:
- all the characters in them are and aren't equal with
==and!=respectively. - the string is greater than other string with
>. - the string is greater than or equal to other string with
>=. - the string is less than other string with
<. - the string is less than or equal to other string with
<=.
- all the characters in them are and aren't equal with
- and other string cannot be checked if they have and don't have their common characters with
bool()and&and withnotand&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).
- all the characters in them with
- can be iterated with a
forstatement. - can be unpacked with an assignment and
forstatement, 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.
- For
- 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.
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
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
v = '' # Empty str
print(v)
# Nothing
v = "Lёт's gφ!" # Let's go!
print(v)
# Lёт's gφ!
v = "I'm John."
v = 'I\'m John.'
v = '''I'm John.'''
v = """I'm John."""
print(v)
# I'm John.
v = '''Apple Orange Banana Kiwi'''
v = 'Apple' " Orange" ''' Banana''' """ Kiwi"""
print(v)
# Apple Orange Banana Kiwi
v = '''Apple
Orange
Banana
Kiwi'''
print(v)
# Apple
# Orange
# Banana
# Kiwi
v = """
Apple
Orange
Banana
Kiwi
"""
print(v)
#
# Apple
# Orange
# Banana
# Kiwi
#
Top comments (0)