No Fluff Guide to Python - P9 - String & Regex
String compare
print('abc' == 'abc')
# True
print('abc' == 'xyz')
# False
This operation is case-sensitive
print('abc' == 'ABC')
# False
print('abc' != 'xyz')
# True
print('abc' != 'abc')
# False
Partial match: in
, not in
in
operator, which determines if one string contains another string
print('bbb' in 'aaa-bbb-ccc')
# True
print('xxx' in 'aaa-bbb-ccc')
# False
print('abc' in 'aaa-bbb-ccc')
# False
print('xxx' not in 'aaa-bbb-ccc')
# True
print('bbb' not in 'aaa-bbb-ccc')
# False
the in
and not in
operators test membership in lists, tuples, dictionaries, and so on
print(1 in [0, 1, 2])
# True
print(100 in [0, 1, 2])
# False
in tuple
, set
, and range
print(1 in (0, 1, 2))
# True
print(1 in {0, 1, 2})
# True
print(1 in range(3))
# True
in
operator for dictionaries (dict
) checks for the presence of a key.
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
print('key1' in d)
# True
print('value1' in d)
# False
Use the values()
and items()
methods to test for the presence of values or key-value pairs.
print('value1' in d.values())
# True
print(('key1', 'value1') in d.items())
# True
print(('key1', 'value2') in d.items())
# False
test whether list B
is a subset of list A
l1 = [0, 1, 2, 3, 4]
l2 = [0, 1, 2]
l3 = [0, 1, 5]
l4 = [5, 6, 7]
print(set(l2) <= set(l1))
# True
print(set(l3) <= set(l1))
# False
String
s = 'aaa-bbb-ccc'
print(s.startswith('aaa'))
# True
print(s.startswith('bbb'))
# False
print(s.endswith('ccc'))
# True
print(s.endswith('bbb'))
# False
print(s.endswith(('aaa', 'bbb', 'ccc')))
# True
Regex: re.search()
, re.fullmatch()
Use re.search()
for partial, forward, and backward matching
import re
s = 'aaa-AAA-123'
print(re.search('aaa', s))
# <re.Match object; span=(0, 3), match='aaa'>
print(re.search('xxx', s))
# None
metacharacter ^
matches the start of the string, and $
matches the end of the string.
print(re.search('^aaa', s))
# <re.Match object; span=(0, 3), match='aaa'>
print(re.search('^123', s))
# None
print(re.search('aaa$', s))
# None
print(re.search('123$', s))
# <re.Match object; span=(8, 11), match='123'>
[A-Z]
represents any single uppercase alphabet letter, and +
means that the previous pattern is repeated one or more times. Thus, [A-Z]+
matches any substring that consists of one or more consecutive uppercase alphabetic characters.
print(re.search('[A-Z]+', s))
# <re.Match object; span=(4, 7), match='AAA'>
Use re.fullmatch()
to check whether the entire string matches a regular expression pattern
s = '012-3456-7890'
print(re.fullmatch(r'\d{3}-\d{4}-\d{4}', s))
# <re.Match object; span=(0, 13), match='012-3456-7890'>
s = 'tel: 012-3456-7890'
print(re.fullmatch(r'\d{3}-\d{4}-\d{4}', s))
# None
\d
represents a number and {n}
represents n
repetitions. Since backslash \
is used in special sequences of regular expressions, such as \d
, it is useful to use raw strings (r''
or r""
) that treat backslashes as literal characters
re.fullmatch()
was added in Python 3.4. In earlier versions, you can use re.search()
with ^
and $
to achieve the same result. You can also use re.match()
and $
, although it is not shown here.
s = '012-3456-7890'
print(re.search(r'^\d{3}-\d{4}-\d{4}$', s))
# <re.Match object; span=(0, 13), match='012-3456-7890'>
s = 'tel: 012-3456-7890'
print(re.search('^\d{3}-\d{4}-\d{4}$', s))
# None
Fetch last char of string
s = '90G'
print(s[len(s) - 1])
v = '90G+'
print(v[len(v) - 1])
G
+
Remove Last Character from String
string_val = "Python Guide"
new_string = string_val[:-1]
print(new_string)
helper function convert G,T to GB and TB
s = '90G'
lchar = s[len(s) - 1]
slchar = ''
multiplier = 1
size_val = 0
if lchar == '+':
slchar = s[len(s) - 2]
if slchar == '':
if lchar == 'G':
multiplier = 1
if lchar == 'T':
multiplier = 1000
size_val = int(s[:-1])
else:
if slchar == 'G':
multiplier = 1
if slchar == 'T':
multiplier = 1000
size_val = int(s[:-2])
Comments
Post a Comment