the built-in functions in the Python string (with code snippets) summarize a
1, find () detection string
Usage format: str1.find (STR2) #在str1中 retrieves whether the string str2 exists, exists, returns the initial address of str2, does not exist, and returns -1.
Str1.find (str2,x) #x是表示下标的变量, starting from the subscript x position to retrieve whether the str2 exists in str1.
Str.find (str2,x,y) #y表示下标整型变量, meaning: Starting from subscript x, detect subscript y End
>>> str1 = "Hello,world,nihao,shijie"
>>> str2 = "ll"
>>> str1.find (str2)
2
>>> str1.find (str2,1)
2
>>> str1.find (str2,2)
2
>>> str1.find (str2,3)
-1
>>> str1.find (str2,1,6)
2
>>> str1.find (str2,1,4)
2
>>> Str1.find (str2,1,3)
-1
2,rfind () reverse (from right to left) detects the last subscript of the string str2
The Str1.rfind (str2) #从 STR1 detects whether str2 exists
Str1.rfind (str2,x,y) # "X,y" to locate the retrieved location, and then retrieves it correctly, then the output string is first subscript.
>>> Str1.rfind (str2)
2
>>> str1.rfind (str2,0)
2
>>> str1.rfind (str2,2)
2
>>> str1.rfind (str2,2,3)
-1
>>> str1.rfind (str2,2,5)
2
3,count () method syntax:
Str.count (str1) #计数 str, the number of substring "str1" appears
Str.count (str1,x) #计数 str, starting with subscript X, the number of substring "str1" appears
Str.count (str1,x,y) #计数 str, starting with subscript X, the number of substring "str1" appears
>>> str = "Hello,world,nihao,shijie"
>>> count (",")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Nameerror:name ' count ' is not defined
>>> str.count (",")
3
>>> str.count ("L")
3
>>> Str.count ("i")
3
>>> str = "Hellohellohello"
>>> str.count (" Llo ")
3
>>> str =" Hellohhhehehhe "
>>> str.count (" he ")
4
4,split () method syntax
Str.split () #默认切片条件为: All empty characters, including spaces, newline (\ n), tab characters (\ t), and so on
**str.split (Str1,num) #切片条件为: Retrieving str1, slicing number to num
* Use the RE module for slicing (with multiple separator characters to retrieve) code as follows:
import re a=str# to be
detected string
x=re.split ( here is the delimiter for multiple inputs , a)
Print (x)
>>> str = "he l l o,wo rld,hi,shijie\t,nihao,\nhi hihi"
>>> str.split () [' he ', ' l ', ' L. ', ' l ',
' O,wo ', ' Rld,hi,shijie ', ', Nihao, ', ' Hi ', ' hihi ']
>>> str
' he l l o,wo rld,hi,shijie\t,nihao,\nhi Hihi '
>>> str.split (",", 2)
[' He l l o ', ' wo rld ', ' Hi,shijie\t,nihao,\nhi hihi ']
>>> str.split (", ", 3)
[' He l l o ', ' wo rld ', ' Hi ', ' shijie\t,nihao,\nhi hihi ']
>>> str.split (" O ", 2)
[' He l l ', ', W ', ' Rld,hi,shijie\t,nihao,\nhi hihi ']
>>> import re
>>> a = "Zhang,shijie, Ni,hao"
>>> x = Re.split (', H J ', a)
>>> print x
[' Zhang,shijie, Ni,hao ']
5,max () method syntax Max (str) #输出字符串中的最大字符
6,min () method syntax min (str)
>>> str = "ABCDSHJSHF"
>>> max (str)
' s '
>>> min (str)
' a '
>> > str = "12345676789"
>>> max (str)
' 9 '
>>> min (str)
' 1 '
7,lower () method syntax:
Str.lower () #直接将字符串str中的所有大写字母转换成小写字母
>>> str = "Zgahhhhihhoioai"
>>> str.lower ()
' Zgahhhhihhoioai '
8,replace () method syntax:
Str.replace (old,new) #直接将老字符串替换为新的字符串
Str.replace (Old,new[,max]) #指定参数max that the number of replacements does not exceed Max;
>>> str = "This
are in" >>> str.replace ("are", ' was ', 1)
' Thwas be in '
>> > Str.replace ("is", ' was ', 2)
' Thwas ', '
9,index () method syntax:
The Str.index (str1) usage is the same as find (), except that if the string you are detecting does not contain what you are looking for, an exception is reported.
>>> Str.index ("is")
2
>>> str.index ("Wax")
traceback (most recent called last):
File "<stdin>", line 1, in <module>
valueerror:substring not found
10,rindex () is similar to index (), with a direction from right to left
11,lstrip () method syntax: Rstrip () Same usage
Str.lstrip () #截掉字符串左边的空格 (how many, how many)
>>> str = "This is,"
>>> Str.lstrip () ' This is ', '
>>> str = ' Zhang ' C10/>>>> Str.rstrip ()
' Zhang '
To sum up so much for the time being. Cond