*Memo:
- My post explains a string (2).
- My post explains str() (1).
- My post explains str() (2).
- My post explains a bytes.
- My post explains a bytearray.
- 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 a list and the list with indexing.
- My post explains a tuple.
- My post explains a set and the set with copy.
- My post explains a frozenset (1).
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a range (1).
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.
- 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 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
.
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
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
#
A string can be used with len()
to get the length as shown below:
v = "Let's go!"
print(len(v))
# 9
An empty string is False
as shown below:
print(bool('')) # Empty str
# False
print(bool(' ')) # str
# True
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
A string can be enlarged with *
and a number as shown below:
v = 'ABCDE' * 3
print(v)
# ABCDEABCDEABCDE
v = '01234' * 3
print(v)
# 012340123401234
v = '' * 3
print(v)
# Nothing
A string can be iterated with a for
statement as shown below:
for v in 'ABC':
print(v)
# A
# B
# C
Be careful, a big string gets OverflowError
as shown below:
v = 'ABCDE' * 1000000000
print(v)
# OverflowError: repeated string is too long
Top comments (0)