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 .
find() Method is used to find a substring in a long string . If you find a substring , Returns the leftmost index of the substring ; Return if not found -1. The format is as follows . (1)str Represents the searched string ; (2)sub Represents the substring of the lookup ; (3)start Start index , When omitted, the default is 0; (4)end Indicates end index , When omitted, it defaults to the length of the string .
str.find(sub[,start[,end]])
example : Find substring “like” Is it in the string new_str in .
new_str = "I like learning Python" # Create string
a=new_str.find("like") # stay new_str Find substring in “like”
b=new_str.find("like",5,15) # stay new_str The index of is 5~15 Find substrings in the characters of
print(a) # Output a
print(b) # Output 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 .
count() Method is used to count the number of occurrences of a substring in a string . This function returns the number of times the substring appears in the string , The format is as follows . (1)str Represents the searched string ; (2)sub Represents the substring of the lookup ; (3)start Start index , When omitted, the default is 0; (4)end Indicates end index , When omitted, it defaults to the length of the string .
str.count(sub[,start[,end]])
example : Create string new_str=“This is a Python book!”, Use count() Methods to find out “is” Number of occurrences .
new_str="This is a Python book!" # Create string "This is a Python book!"
a=new_str.count('is') # Statistics new_str in “is” Number of occurrences
b=new_str.count('is',1,6) # Set the start and end indexes , Statistics “is” Number of occurrences
print(a) # Output a
print(b) # Output b
The operation results are as follows :
split() Method takes the specified character as the separator , Start at the left end of the string and separate it into multiple strings , And return a list of delimited results . (1)str Represents a delimited string ; (2)delimiter Represents a separator , When omitted, the default is null character , Including Spaces 、 Line break (\n)、 tabs (\t) etc. ; (3)num Indicates the number of divisions , If omitted, all are divided by default .
str.split([delimiter,num])
example : Create string new_str = “This is an example of cutting”, Use split() Segmentation .
new_str = "This is an example of cutting" # Create string
print(new_str.split())
print(new_str.split(' ', 3))
The operation results are as follows :
join() Method is used to connect elements in a sequence with a specified character , Generate a new string . (1)str Represents a connector , Can be null ; (2)sequence Represents the sequence of elements to be connected .
str.join(sequence)
example 1: Create string new_str = “This is a python book!”, Use join() Methods will new_str Characters in “-” Connect .
new_str = "This is a python book!" # Create string This is a python book!
a='-'.join(new_str) # use “-” Connect new_str The characters in
print(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 find(),count(),split(),join() Method . You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .