function
The find() method detects whether the substring str is included in the string. If the beg and end ranges are specified, it checks whether it is included in the specified range. If the specified range contains the specified index value, it returns the starting position of the index value in the string.If the index value is not included, return -1.
string='abcde' x=('a') y=('bc') z=('f') print(x) print(y) print(z) #Run result0 1 -1
function
The index() method detects whether the substring str is included in the string. If the beg (start) and end (end) ranges are specified, it checks whether it is included in the specified range. This method is the same as the python find() method.But if str is not in string, an exception will be reported.
string='abcde' x=('a') y=('bc') #z=('f') print(x) print(y) #print(z) 0 1 ValueError: substring not found
function
The Python join() method is used to convertsequenceThe elements inSpecified charactersThe concatenation generates a new string.
lis=['a','b','c','d','e'] string='abcde' tup=('a','b','c','d','e') print(''.join(lis)) print(' '.join(string)) print('$'.join(tup)) #Run resultabcde a b c d e a$b$c$d$e
Note that the elements in the sequence must be strings, not numbers.
function
split() slices the string by specifying the separator, and if the second parameter num has a specified value, it is divided into num+1 substrings.
(str="", num=(str))
string='this is an interesting story!' a=() b=(' ',2) c=('s') d=(',') print(a) print(b) print(c) print(d) #Run result['this', 'is', 'an', 'interesting', 'story!'] ['this', 'is', 'an interesting story!'] ['thi', ' i', ' an intere', 'ting ', 'tory!'] ['this is an interesting story!']
function
The Python strip() method is used to remove characters specified at the beginning and end of a string (default is a space) or character sequence.
Notice: This method can only delete characters at the beginning or end, and cannot delete characters at the middle part.
string='**this is an ***interesting story!***' a=('*') b=('*') c=('*') print(string) print(a) print(b) print(c) #Run result**this is an ***interesting story!*** this is an ***interesting story! this is an ***interesting story!*** **this is an ***interesting story!
lstrip and rstrip remove the specified characters on the left and right respectively.
This is the end of this article about the details of the difference between index and find of python strings. For more information about the difference between index and find of python strings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!