*Memo:
- My post explains a string (1).
- My post explains a string (3).
- My post explains a string (4).
- My post explains str() (1).
- My post explains str() (2).
A string can be used with len() to get the length as shown below:
v = "Let's go!"
print(len(v))
# 9
A non-empty string and empty string are:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotrespectively.
# Non-empty str
print(bool('0'))
print(bool(' '))
# True
# Empty str
print(bool(''))
# False
# Non-empty str
print(not '0')
print(not ' ')
# False
# Empty str
print(not '')
# True
A string can be checked if specific characters are and aren't in the string with in and not in respectively as shown below:
v = 'ABC'
print('A' in v)
print('BC' in v)
print('ABC' in v)
print('' in v)
# True
print('a' in v)
# False
v = 'ABC'
print('A' not in v)
print('BC' not in v)
print('ABC' not in v)
print('' not in v)
# False
print('a' not in v)
# True
A string can be checked if the string is and isn't referred to by two variables with is and is not respectively as shown below:
*Memo:
- Be careful, string literals with
isandis notget warnings so use==and!=respectively.
v1 = 'ABC'
v2 = 'ABC'
v3 = v1
print(v1 is v2) # True
print(v1 is v3) # True
print(v1 is not v2) # False
print(v1 is not v3) # False
print('ABC' is 'ABC') # True
print('ABC' is 'AB') # False
# SyntaxWarning: "is" with 'str' literal. Did you mean "=="?
print('ABC' is not 'ABC') # False
print('ABC' is not 'AB') # True
# SyntaxWarning: "is not" with 'str' literal. Did you mean "!="?
print('ABC' == 'ABC') # True
print('ABC' == 'AB') # False
print('ABC' != 'ABC') # False
print('ABC' != 'AB') # True
A string 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
<=.
*Memo:
- Lexicographical comparison is used with their strings:
- Lexicographical comparison:
- compares each character's Unicode points in their strings from their 1st characters one by one:
- finishes the comparison:
- just after the current comparison which has a difference between two characters in their strings is done.
- or just after the current comparison of the character and absent character and vice versa in their strings is done:
- An absent character is evaluated to a lower value than the other.
- or if both of their strings are exhausted.
- E.g.
'ABC' > 'ABD'isFalse, comparing'C' > 'D'. - E.g.
'ABC' > 'AB'isTrue, comparing'C' > an absent character(lower value than the other). - E.g.
'ABC' > 'AD'isFalse, comparing'B' > 'D'.
- Lexicographical comparison:
v = 'ABC' # 65 66 67
print(v == 'ABC') # 65 66 67 # True
print(v == 'CBA') # 67 66 65 # False
print(v == 'ABD') # 65 66 68 # False
print(v == 'AB@') # 65 66 64 # False
print(v == 'AB') # 65 66 # False
print(v == 'AD') # 65 68 # False
print(v == 'A@') # 65 64 # False
print(v == 'ABCD') # 65 66 67 68 # False
print(v == 'ABDE') # 65 66 68 69 # False
print(v == 'AB@?') # 65 66 64 63 # False
print(v == '') # False
v = 'ABC' # 65 66 67
print(v != 'ABC') # 65 66 67 # False
print(v != 'CBA') # 67 66 65 # True
print(v != 'ABD') # 65 66 68 # True
print(v != 'AB@') # 65 66 64 # True
print(v != 'AB') # 65 66 # True
print(v != 'AD') # 65 68 # True
print(v != 'A@') # 65 64 # True
print(v != 'ABCD') # 65 66 67 68 # True
print(v != 'ABDE') # 65 66 68 69 # True
print(v != 'AB@?') # 65 66 64 63 # True
print(v != '') # True
v = 'ABC' # 65 66 67
print(v > 'ABC') # 65 66 67 # False
print(v > 'CBA') # 67 66 65 # False
print(v > 'ABD') # 65 66 68 # False
print(v > 'AB@') # 65 66 64 # True
print(v > 'AB') # 65 66 # True
print(v > 'AD') # 65 68 # False
print(v > 'A@') # 65 64 # True
print(v > 'ABCD') # 65 66 67 68 # False
print(v > 'ABDE') # 65 66 68 69 # False
print(v > 'AB@?') # 65 66 64 63 # True
print(v > '') # True
v = 'ABC' # 65 66 67
print(v >= 'ABC') # 65 66 67 # True
print(v >= 'CBA') # 67 66 65 # False
print(v >= 'ABD') # 65 66 68 # False
print(v >= 'AB@') # 65 66 64 # True
print(v >= 'AB') # 65 66 # True
print(v >= 'AD') # 65 68 # False
print(v >= 'A@') # 65 64 # True
print(v >= 'ABCD') # 65 66 67 68 # False
print(v >= 'ABDE') # 65 66 68 69 # False
print(v >= 'AB@?') # 65 66 64 63 # True
print(v >= '') # True
v = 'ABC' # 65 66 67
print(v < 'ABC') # 65 66 67 # False
print(v < 'CBA') # 67 66 65 # True
print(v < 'ABD') # 65 66 68 # True
print(v < 'AB@') # 65 66 64 # False
print(v < 'AB') # 65 66 # False
print(v < 'AD') # 65 68 # True
print(v < 'A@') # 65 64 # False
print(v < 'ABCD') # 65 66 67 68 # True
print(v < 'ABDE') # 65 66 68 69 # True
print(v < 'AB@?') # 65 66 64 63 # False
print(v < '') # False
v = 'ABC' # 65 66 67
print(v <= 'ABC') # 65 66 67 # True
print(v <= 'CBA') # 67 66 65 # True
print(v <= 'ABD') # 65 66 68 # True
print(v <= 'AB@') # 65 66 64 # False
print(v <= 'AB') # 65 66 # False
print(v <= 'AD') # 65 68 # True
print(v <= 'A@') # 65 64 # False
print(v <= 'ABCD') # 65 66 67 68 # True
print(v <= 'ABDE') # 65 66 68 69 # True
print(v <= 'AB@?') # 65 66 64 63 # False
print(v <= '') # False
A string and other string cannot be checked if they have and don't have their common characters with bool() and & and with not and & respectively as shown below:
v = 'ABC'
print(bool(v & 'BD'))
print(not (v & 'BD'))
# TypeError: unsupported operand type(s) for &: 'tuple' and 'tuple'
Top comments (0)