Python learning notes (7) Python string (3), learning notes python
Common string Methods
Split () Splits a string, specifying a separator to separate the string
Join () Concatenates the elements in the sequence with specified characters to generate a new string.
Str. strip () is used to remove the specified character (space by default) from the string header and tail)
Str. lstrip () is used to intercept spaces or specified characters on the left of the string.
Str. rstrip () is used to intercept spaces or specified characters on the right side of a string.
Str. upper () converts string to uppercase
Str. lower () converts string to lowercase
Str. capitalize () converts the first letter of the string to uppercase.
Str. isupper () determines whether it is capital
Str. islower () determines whether it is lowercase
Str. istitle () determines whether it is the title
Example:
1 >>> a = "Hello World" # use a space as the separator to split the string and return a list 2 >>>. split ("") 3 ['hello', 'World'] 4 >>> B = "www.baidu.com" # Use the dot. returns a list of 5> B. split (". ") 6 ['www ', 'baidu', 'com'] 7 >>> c =. split ("") 8 >>> c 9 ['hello', 'World'] 10 >>> "-". join (c) # join each element in the list with a minus sign. 11 'hello-world' 12 >>> "*". join (B. split (". ") 13 'www * baidu * com' 14 >>> a # The original string has not changed. 15 'Hello world' 16 >>>
Remove Spaces
1 >>> x = "hello" 2 >>> x. strip () # Remove spaces on both sides of the left and right. 3 'hello' 4 >>> x. lstrip () # Remove the left space 5 'hello' 6 >>> x. rstrip () # Remove the space at the right side'
Note: The original string does not change. Removing spaces is equivalent to recreating a new string, which is the same as split ().
Case sensitivity
1 >>> a2 'Hello world' 3 >>>. upper () # convert to uppercase 4 'Hello world' 5 >>>. lower () # convert to lowercase 6 'Hello world' 7 >>>. capitalize () # convert the initial letter to 'Hello world' 9>