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 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 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
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 and is not respectively as shown below:

*Memo:

  • Be careful, string literals with is and is not 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 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' is False, comparing 'C' > 'D'.
    • E.g. 'ABC' > 'AB' is True, comparing 'C' > an absent character(lower value than the other).
    • E.g. 'ABC' > 'AD' is False, comparing 'B' > 'D'.
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
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 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)