Summary of usage of str built in functions in python
- 2021-08-31 08:31:01
- OfStack
In the process of using python, we should often encounter str built-in functions when typing code. In order to prevent confusion, this paper summarizes str built-in functions. 1 String lookup classes: find, index; 2. String judgment classes: islower, isalpha;; 3. Content judgment classes: tartswith, endswith;; 4. Operation class functions: format, strip and join.
1. String lookup classes: find, index
find and index both look up whether a string contains a substring;
The difference between the two is that index will report an error if it can't find a string, while find will return-1;
rfind, lfind are left-start lookups or right-start lookups.
2. String judgment classes: islower, isalpha
The characteristic of this kind of function is the beginning of is
isalpha: To judge whether it is a letter, you should pay attention to two points:
The default premise of this function is that the string contains at least 1 character. If it does not, it returns false
Chinese characters are considered as alpha. This function cannot distinguish English letters from Chinese characters. Please use unicode code to distinguish Chinese and English
Function of isdigit, isnumeric and isdecimal
islower determines if it is lowercase
3. Content judgment class
startswith, endswith: Does it begin or end with XXX
4. Operate class functions
format: Formatting function
strip: Delete characters on either side of the string (default space). You can specify characters instead of deleting one, but qualifying consecutive characters from scratch.
rstrip, lstrip Delete the right/left character.
join: Splicing strings
s1='$'
s2='-'
s3=' '
ss=['Today','is','a','good','day']
print(s1.join(ss))
Today$is$a$good$day
print(s2.join(ss))
Today-is-a-good-day
print(s3.join(ss))
Today is a good day
Instance extension:
>>>s = 'RUNOOB'
>>> str(s)
'RUNOOB'
>>> dict = {'runoob': 'runoob.com', 'google': 'google.com'};
>>> str(dict)
"{'google': 'google.com', 'runoob': 'runoob.com'}"
>>>