Thirteen most commonly used string processing operations in python
- 2021-09-24 22:49:47
- OfStack
Preface
Bloggers have been learning python for several years, and their mastery of python is getting deeper and deeper. Most of the time, they hope to master more and more knowledge of python. However, they also realize that it is more important to be proficient in basic things than to know more knowledge.
Today, let's talk about python string processing
First, we define two strings, and then we will demonstrate them in a series of operations
str1="sadf AVD"
str2="JIK dojfa kldfj"
Step 1 Convert lowercase letters to uppercase
print(str2.upper())
print(str1.upper())
Results:
JIK DOJFA KLDFJ
SADF AVD
2. Convert uppercase letters to lowercase
print(str1.lower())
print(str2.lower())
Results:
sadf avd
jik dojfa kldfj
3. Replace substrings in a string
print(str1.replace("sa","dfahj"))
Results:
dfahjdf AVD
4. Convert uppercase to lowercase. Convert lowercase to uppercase
print(str1.swapcase())
Results:
SADF avd
5. Make a new string length, and fill in the insufficient part with the made characters
print(str1.center(15,"="))
Results:
====sadf AVD===
6. Make a separator to divide the string
print(str2.split())
print("dsfahjosio idfji jodfhai fjhako ifjda dijsf".split('a'))
Results:
print(str2.split())
print("dsfahjosio idfji jodfhai fjhako ifjda dijsf".split('a'))
7. Remove string header and tail specific strings
print(" dsfa dfjik ".strip())
Results:
str3="sdfij odfhjodj 0fj odjfh oidfj iofdj"
8. Count the number of substrings occurring
str3="sdfij odfhjodj 0fj odjfh oidfj iofdj"
print(str3.count('j'))
Results:
7
9. Find the leftmost string and return the corresponding subscript
print(str3.find('j'))
Results:
4 (returned-1 if not found)
10. Determine if all strings are letters
print(str2.upper())
print(str1.upper())
0
Results:
False
True
11. Determine if each element in the string is a decimal number, including full angle
print(str2.upper())
print(str1.upper())
1
Results:
False
True
12. Determine if all strings are uppercase
print(str2.upper())
print(str1.upper())
2
Results:
False
13. Determine if all strings are lowercase
print(str1.islower())
Results:
False
Summarize