1. Capitalize first letter
Name = "Wuyuchao"
result = Name.capitalize ()
Print (Result)
Back to Wuyuchao
------------------------------------------------------------------------------------------------------------
2. Center Content
Name = "Wuyuchao"
result = Name.center ("*") #20代表长度, * represents the fill character
Print (Result)
Back to ******wuyuchao******
------------------------------------------------------------------------------------------------------------
3. Number of sub-sequences
Name = "Wuyuchao"
result = Name.count (' Yu ')
Back 1 Yu has appeared 1 times
RESULT2 = Name.count (' nnn ')
Print (RESULT2)
Returns 0 nnn not appearing
------------------------------------------------------------------------------------------------------------
4. Coding
Name = "Wuyushu"
result = Name.encode ("GBK")
Print (Result)
------------------------------------------------------------------------------------------------------------
5, Case conversion
Name = "Wuyuchao"
result = Name.swapcase ()
Print (Result)
Back to Wuyuchao
------------------------------------------------------------------------------------------------------------
6, whether to end the * * *
Name = "Wuyuchao"
RESULT1 = Name.endswith ("o")
RESULT2 = Name.endswith ("x")
Print (RESULT1,RESULT2)
Returns True False
------------------------------------------------------------------------------------------------------------
7, whether to start with * * *
Name.startswith ()
------------------------------------------------------------------------------------------------------------
8. Number of occurrences of a subsequence
Name = "Wwuuyyuucchhaaoo"
result = Name.count (' u ')
Print (Result)
Returns 4, 4 occurrences of u in a string
------------------------------------------------------------------------------------------------------------
9. Replace tab with 8 spaces
Name = "W\tuyuchao"
Print (name) returns w Uyuchao
result = Name.expandtabs ()
Print (Result) returns W Uyuchao
------------------------------------------------------------------------------------------------------------
10, query the position of the subsequence in the string, if not, return-1
Name = "Wuyuchao"
result = Name.find ("W")
RESULT2 = Name.find ("h")
RESULT3 = Name.find ("x")
Print (RESULT,RESULT2,RESULT3)
Returns W 0, H 5, x-1 the string does not contain x return-1
------------------------------------------------------------------------------------------------------------
11, query the position of the subsequence in the string, if not, direct error
Name = "Wuyuchao"
result = Name.index ("W")
RESULT2 = Name.index ("h")
Return W 0, H 5
RESULT3 = Name.index ("x")
Direct error
------------------------------------------------------------------------------------------------------------
12. String formatting
Name = "Wu{0}{1}" #数字从0开始
result = Name.format ("Yu", "Chao")
Print (Result)
Back to Wuyuchao
Name = "Wu{ming}{zi}"
result = Name.format (ming= "Yu", zi= "Chao")
Print (Result)
Back to Wuyuchao
------------------------------------------------------------------------------------------------------------
13, whether it is letters and numbers
Name = "Wuyuchao123"
Print (Name.isalnum ())
Returns True
------------------------------------------------------------------------------------------------------------
14, whether it is a letter
Name = "FABACASD"
Print (Name.isalpha ()) returns True
name2 = "FABACASD1"
Print (Name2.isalpha ()) returns false
------------------------------------------------------------------------------------------------------------
15, whether the number
Name = "1"
Print (Name.isdigit ())
True
Name = "a"
Print (Name.isdigit ())
False
------------------------------------------------------------------------------------------------------------
16. Is lowercase
Name.islower ()
------------------------------------------------------------------------------------------------------------
17. Whether it is capitalized
Name.isupper ()
------------------------------------------------------------------------------------------------------------
18. String connection
Li = [' W ', ' u ', ' y ', ' u ', ' C ', ' h ', ' A ', ' O ']
result = "". Join (LI)
Print (Result)
Back to Wuyuchao
------------------------------------------------------------------------------------------------------------
19. Right alignment of content
Name = "Wuyuchao"
result = Name.rjust (20)
Print (Result)
Back to Wuyuchao
------------------------------------------------------------------------------------------------------------
20. Convert to lowercase
Name = "Wuyuchao"
result = Name.swapcase ()
Print (Result)
Back to Wuyuchao
------------------------------------------------------------------------------------------------------------
21, remove the right blank
Name = "Wuyuchao"
Print (name)
result = Name.rstrip ()
Print (Result)
The right side of name has a space
No space on the right side of result
------------------------------------------------------------------------------------------------------------
22, split, before, during and after three parts
Name = "Wuyuchao"
result = Name.partition ("Yu")
RESULT2 = name.partition ("WU")
Print (RESULT,RESULT2)
Return (' Wu ', ' Yu ', ' Chao ') (', ' Wu ', ' Yuchao ')
------------------------------------------------------------------------------------------------------------
23. Replace
Name = "Wuyuchao"
result = Name.replace ("Yu", "Yu")
Print (Result)
Back to Wuyuchao
------------------------------------------------------------------------------------------------------------
Methods for Python strings