Match word in list regex

Compare strings in Python [exact match, partial match, etc.]

Posted: 2021-09-06 / Tags: Python, String, Regular expression
Tweet

This article describes how to compare strings str in Python.

  • Exact match [equality comparison]: ==, !=
  • Partial match: in, not in
  • Forward / Backward match: startswith[], endswith[]
  • Order comparison: =
  • Case-insensitive comparison: upper[], lower[]
  • Regular expressions: re.search[], re.fullmatch[]
Sponsored Link

Exact match [equality comparison]: ==, !=

As with numbers, the == operator is used to determine if two strings are equal. If they are equal, True is returned, and if they are not, False is returned.

print['abc' == 'abc'] # True print['abc' == 'xyz'] # False
source: str_compare.py

It is case-sensitive. The same applies to comparisons with other operators and methods. See below for a case-insensitive comparison.

print['abc' == 'ABC'] # False
source: str_compare.py

! = returns True if they are not equal, and False if they are equal.

print['abc' != 'xyz'] # True print['abc' != 'abc'] # False
source: str_compare.py

Partial match: in, not in

Use the in operator for partial matches, i.e., whether one string contains the other string.

x in y returns True if x is contained in y [x is a substring of y], False if it is not. If each character of x is contained in y discretely, False is returned.

print['bbb' in 'aaa-bbb-ccc'] # True print['xxx' in 'aaa-bbb-ccc'] # False print['abc' in 'aaa-bbb-ccc'] # False
source: str_compare.py

not in returns True if it is not included, False if it is included.

print['xxx' not in 'aaa-bbb-ccc'] # True print['bbb' not in 'aaa-bbb-ccc'] # False
source: str_compare.py

in and not in are also used to check the existence of elements in a list. See the following article for details.

  • in operator in Python [for list, string, dictionary, etc.]

Forward / Backward match: startswith[], endswith[]

Use the string method startswith[] for forward matching, i.e., whether a string starts with the specified string.

  • Built-in Types - str.startswith[] Python 3.9.7 documentation
s = 'aaa-bbb-ccc' print[s.startswith['aaa']] # True print[s.startswith['bbb']] # False
source: str_compare.py

You can also specify a tuple of strings as an argument.

True is returned if the string starts with one of the elements of the tuple, and False is returned if the string does not start with any of them. Note that an error will occur if you specify a list instead of a tuple.

print[s.startswith[['aaa', 'bbb', 'ccc']]] # True print[s.startswith[['xxx', 'yyy', 'zzz']]] # False # print[s.startswith[['a', 'b', 'c']]] # TypeError: startswith first arg must be str or a tuple of str, not list
source: str_compare.py

Use the string method endswith[] for backward matching, i.e., whether a string ends with the specified string. Its usage is the same as startswith[].

  • Built-in Types - str.endswith[] Python 3.9.7 documentation
print[s.endswith['ccc']] # True print[s.endswith['bbb']] # False print[s.endswith[['aaa', 'bbb', 'ccc']]] # True
source: str_compare.py
Sponsored Link

Order comparison: =

Strings can be compared with the = operators as well as numbers. They are compared in lexical order.

print['a'

Chủ Đề