About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python String related methods .
replace() Method is used to replace an old string with a new one in a string .. The format is as follows . (1)str Represents the searched string ; (2)old Represents the substring to be replaced ; (3)new Represents a new string , Used for replacement old Substring ; (4)max Is an optional parameter , Indicates that the replacement does not exceed max Time , Replace all by default when omitted .
str.replace(old,new[,max])
example : Create string “new_str = ”Monday Tuesday Wednesday Thursday Friday Saturday Sunday“”, Use replace() Method to replace day by DAY.new_str in .
new_str = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
a=new_str.replace('day','DAY') # take new_str Medium day Replace with DAY
b=new_str.replace('day','DAY',3) # take new_str Medium day Replace with DAY, Limit to 3 Time
print("a:",a)
print("b:",b)
The operation results are as follows :
Another common method for finding substrings is index() Method , The method and find() The usage of methods is basically the same , The difference is that when the substring you are looking for does not exist , Throw an exception .
strip() Method is used to delete consecutive white space characters or specified characters at both ends of a string , The format is as follows . (1)str Representation string ; (2)chars Means to remove the specified characters at both ends of the string , When omitted, the default is space .
str.strip([chars])
example : Create string new_str = “110This is an example 0001”, Use strip() Method to remove... From the string “0” and “1”.
new_str = "110This is an example 0001" # Create string
a=new_str.strip('1') # remove new_str On both sides 1
b=new_str.strip('01') # remove new_str On both sides 0 and 1
print("a:",a)
print("b:",b)
The operation results are as follows :
lower() Method is used to convert all uppercase characters in a string to lowercase .
str.lower()
example : Create string “new_str=”This is an EXAMPLE“”, Use lower() Method to convert it to lowercase .
new_str = "This is an EXAMPLE" # Create string
a=new_str.lower() # take new_str Convert uppercase characters in to lowercase
print("a:",a)
The operation results are as follows :
upper() Method is used to convert all lowercase characters in the string to uppercase .
str.lower()
example : Create string “new_str=”This is an EXAMPLE“”, Use upper() Method to convert it to uppercase .
new_str = "This is an EXAMPLE" # Create string
a=new_str.upper() # take new_str Convert lowercase characters in to uppercase
print("a:",a)
The operation results are as follows :
isalnum() Method is used to detect whether a string is composed of letters and numbers , Or a combination of two . If so, go back True; Otherwise return to False.
str.isalnum()
example 1: Create string new_str=“2018example”, Use isalnum() Methods to judge new_str Whether the consists of letters or numbers .
new_str = "2018example" # Create string
a=new_str.isalnum() # Judge new_str Whether there are only numbers or letters in
print("a:",a)
The operation results are as follows :
example 2: The string “This is a python book!“ Delete the extra space in , That is, if there are consecutive spaces, only one .
new_str = "This is a python book!" # Create string
s_str=new_str.split() # Use empty characters as separators , take new_str Divide it all
print(s_str) # Output the result after segmentation
j_str=' '.join(s_str) # Connect... With spaces s_str The characters in
print(j_str) # Output the connected string
The operation results are as follows :
1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial
The above is about Python Knowledge of string methods , There are mainly replace(),strip(),lower(),upper(),isalnum() Method . You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .