DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String in Python (2)

Buy Me a Coffee

*Memo:

A string can be used with len() to get the length as shown below:

v = "Let's go!"

print(len(v))
# 9
Enter fullscreen mode Exit fullscreen mode

A non-empty string and empty string are:

  • True and False, checking them with bool() respectively.
  • False and True, inverting their truth values with not keyword respectively.
# Non-empty str
print(bool('0'))
print(bool(' '))
# True

# Empty str
print(bool(''))
# False
Enter fullscreen mode Exit fullscreen mode
# Non-empty str
print(not '0')
print(not ' ')
# False

# Empty str
print(not '')
# True
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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 is keyword and with is and not keyword 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
Enter fullscreen mode Exit fullscreen mode
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 "!="?
Enter fullscreen mode Exit fullscreen mode
print('ABC' == 'ABC') # True
print('ABC' == 'AB')  # False

print('ABC' != 'ABC') # False
print('ABC' != 'AB')  # True
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)