String processing related API
Strings are one of seven sequence types.
In addition to sequence manipulation functions, such as len() to find the length of a string, Python also provides an API rich enough to write an editor for strings.
Lookup class API
First of all, the following lookup APIs are all for finding a location, or a total of several such operations.
If you just want to determine whether a string is a substring of another string, you can use the in operator of sequences.
example:
str1 = "hahahahaha,heiheihei"
if 'haha' in str1:
print('haha is a substring of '+str1)
else:
print('haha is NOT a substring of '+str1)
Count how many matching strings there are - count function
Prototype: str.count(sub[, start[, end]])
* sub: substring to find
* start: start position
* end: end position
The count function of the string can count how many times there are matches. Let's look at an example, there are 5 ha and 3 hei
str1 = "hahahahaha,heiheihei"
print(str1.count('ha'))
print(str1.count('haha'))
The output is 5 and 2.
haha can only match twice.
Plus extended parameters:
print(str1.count('ha',1))
print(str1.count('ha',0,5))
find and rfind without throwing exceptions
The behavior of the find function is to return the coordinates in the sequence if it can be found, and -1 if it can't be found. rfind searches from right to left. Let's look at an example:
print(str1.find('haha'))
print(str1.rfind('haha'))
The output values are 0 and 6.
Examples not found:
print(str1.find('hoho'))
print(str1.rfind('hi'))
The output values are all -1.
Full form:
* str.find(sub[, start[, end]])
* str.rfind(sub[, start[, end]])
Lookups that throw exceptions: index and rindex
index and rindex function basically the same as find and rfind, except that ValueError is thrown instead of returning -1 when not found.
example:
>>> str1 = "hehe"
>>> str1.index('ha')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
So we need to add try...except statements to handle it:
try:
str1.index('hoho')
except ValueError:
print('hoho cannot be found in '+str1)
Startswith, endswith
Sometimes, we want to do a de novo match or a tail match. At this time, we will use the startswith function and the endswith function. example:
print(str1.startswith('ha'))
print(str1.endswith('hei'))
Both return values are True.
If you need more complex matching, you still need regular expressions. Unlike languages such as Java, regular expressions in Python have a dedicated module, and the string API is not responsible for this.
summary
- Determine whether a string can be found in another string: in
- Determine whether the left matches: startswith
- Determine whether the right match: endswith
- Count how many times it can match: count
- Find the matching position without throwing an exception: find, rfind
- An exception will be thrown to find the matching position: index, rindex