*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 withnotkeyword respectively.
# 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 keyword and with not and in keyword 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 keyword and with is and not keyword respectively as shown below:
*Memo:
- Be careful, string literals with iskeyword and withisandnotkeyword get 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 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 >.
v = 'ABC'
print(v == 'ABC')  # True
print(v == 'ABD')  # False
print(v == 'AB')   # False
print(v == 'AD')   # False
print(v == 'ABCD') # False
print(v == 'ABDE') # False
print(v == '')     # False
v = 'ABC'
print(v != 'ABC')  # False
print(v != 'ABD')  # True
print(v != 'AB')   # True
print(v != 'AD')   # True
print(v != 'ABCD') # True
print(v != 'ABDE') # True
print(v != '')     # True
v = 'ABC'
print(v <= 'ABC')  # True
print(v <= 'ABD')  # True
print(v <= 'AB')   # False
print(v <= 'AD')   # True
print(v <= 'ABCD') # True
print(v <= 'ABDE') # True
print(v <= '')     # False
v = 'ABC'
print(v >= 'ABC')  # True
print(v >= 'ABD')  # False
print(v >= 'AB')   # True
print(v >= 'AD')   # False
print(v >= 'ABCD') # False
print(v >= 'ABDE') # False
print(v >= '')     # True
v = 'ABC'
print(v < 'ABC')  # False
print(v < 'ABD')  # True
print(v < 'AB')   # False
print(v < 'AD')   # True
print(v < 'ABCD') # True
print(v < 'ABDE') # True
print(v < '')     # False
v = 'ABC'
print(v > 'ABC')  # False
print(v > 'ABD')  # False
print(v > 'AB')   # True
print(v > 'AD')   # False
print(v > 'ABCD') # False
print(v > 'ABDE') # False
print(v > '')     # True
A string 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 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)