In common data types, we can use quotation marks ('or ") to create a string, and use and manipulate the string, which requires specific functions. The following are common Python string manipulation methods:
1. Capital ()
Function: capitalize () is mainly used to realize the function of capitalizing the first letter of a string and lowercase other letters.
Example:
1
2str 1 = "oldboy "
print(str 1 . capital())
Output result: old boys
2. Exchange situation ()
Function: swapcase () is mainly used to realize string case inversion.
Example:
1
2str 1 = " Oldboy "
print(str 1.swapcase())
Output result: old boys
3. Title ()
Function: title () is mainly used to realize the non-letter-separated part of a string, with the first letter capitalized and the rest lowercase.
Example:
1
2str 1 = "old boys edu com"
print(str 1.title())
Output result: old boys Edu Com
4. Upper part ()
Function: upper () is mainly used to capitalize all the letters of a string.
Example:
1
2str 1 = "Oldboyedu "
print(str 1.upper())
Output result: OLDBOYEDU
5. Lower part ()
Function: lower () is mainly used to realize that all letters of a string are lowercase.
Example:
1
2str 1 = "oLDBOYEDU "
Print (str 1.lower ())
Output result: oldboyedu
6. Center ()
Function: center () is mainly used to center the string content, and the filler is empty by default.
Example:
1
2
3str 1 = "Oldboyedu "
Print (str 1.center( 15))
print(str 1 . center( 15," * "))
Output result:
Aldboyedu
***Oldboyedu***
7. Find ()
Function: The main function of find () is to find the index by elements, which can be found in whole or in pieces. If it cannot be found, it will return-1.
Example:
1
2
3str 1 = "Oldboyedu "
print(str 1.find('b '))
print(str 1.find('A '))
Output result: 3-1
8. Index ()
Function: The main function of index () is to find the index by elements, which can be searched as a whole or sliced. If it is not found, an error will be reported.
Example:
1
2
3str 1 = " Oldboyedu "
print(str 1.index("b "))
print(str 1.index("A "))
Output result:
Backtracking (last call):
File "",line 1, located at
Wrong value: substring not found.
9. Start with (obj)
Function: The main function of startswith(obj) is to check whether the string starts with obj, and if so, return True, otherwise, return False.
Example:
1
2str 1 = "Oldboyedu "
print(str 1.startswith("O "))
Output result: true
10.endswith(obj)
Function: endswith(obj) is mainly used to check whether the string starts with obj, and if so, it returns True, otherwise it returns False.
Example:
1
2str 1 = " Oldboyedu "
Print (str 1.endswith("edu "))
Output result: true
1 1. Strip ()
Function: The main function of strip () is to remove spaces or other characters, line breaks, tab keys, etc.
Example:
1
2
three
4 str1= "* * old boys * * *"
Print(str 1.strip("*")) # Remove the * sign on both sides.
Print(str 1. lstrip(" * ")# Delete the left *.
Print (str1.rstrip ("*")) # Remove the * sign on the right.
Output result:
old boy
Old boys * * *
** * old boys
12. substitution (oldstr, newstr)
Function: replace(oldstr, newstr) is mainly used to replace strings.
Example:
1
2str 1 = "Oldboyedu "
print(str 1.replace("boy "," man "))
Output result: Oldmanedu
13. Issafa ()
Function: The main function of isalpha () is to judge whether a string consists of only letters, whether it returns true or not, and whether it returns False.
Example:
1
2
three
4str 1 = "Oldboyedu "
Str2 = "old boys edu"
print(str 1.isalpha())
print(str2.isalpha())
Output results: true and false
14.isdigit()
Function: isdigit () is mainly used to judge whether the string is only composed of numbers and whether it returns true.
Example:
1
2
three
4str 1 = "Oldboyedu "
str2 = "520 "
print(str 1.isdigit())
print(str2.isdigit())
Output result: false and true
15. Format ()
Function: The main function of format () is to format strings.
Method 1: Pass parameters by position.
1
2str 1 =' My name is {} and I am {} years old'. Format ("old boys", 30)
Print (str 1)
Output: My name is oldboy, and I am 30 years old.
Method 2: Pass parameters by index.
1
2str 1 =' My name is {0} and I am {1} years old'. Format ('old boys', 30).
Print (str 1)
Output: My name is oldboy, and I am 30 years old.
Method 3: Press the key to pass the parameters.
1
2str 1 =' My name is {name} and I am {age} years old'. Format (age =30, name ='oldboy').
Print (str 1)
Output: My name is oldboy, and I am 30 years old.
16. Count ()
Function: The main function of count () is to count the number of times an element appears in a string.
1
2str 1 = "oldboyedu "
Print (string 1. Count(' o ')# Counts the number of occurrences of the character o in a string.
Data result: 2