This article introduces the function example of the python3 string to describe the add function (append a string later)
S1 = 'hello' s2 = s1. _ add _ ('Boy! ') Print (s2) # output: Hello boy!
Contains (returns True if a string is included)
S1 = 'hello' result = s1. _ contains _ ('hes') print (result) # output: True
Eq (returns True if two strings are the same)
S1 = 'hello' s2 = 'who' result = s1. _ eq _ (s2) print (result) # output: False
Format
# Placeholder
Getattribute
# Placeholder
Getitem
# Placeholder
Getnewargs
# Placeholder
Ge (greater than or equal)
Print ('B'. ge ('A') # output: True
Gt (greater)
Print ('B'. ge ('A') # output: True
Hash
# Placeholder
Iter
# Placeholder
Len (returns the string length)
Print ('ABC'. len () # output: 3
Le (less than or equal)
Print ('B'. le ('A') # output: False
Lt (less)
Print ('B'. lt ('A') # output: False
Mod
# Placeholder
Mul
# Placeholder
New
# Placeholder
Ne
# Placeholder
Repr
# Placeholder
Rmod
# Placeholder
Rmul
# Placeholder
Sizeof
# Placeholder
Str (return user)
Print ('ABC'. _ str _ () # output: abc
Capitalize (uppercase)
S = 'Tom 'print (s. capitalize () # output: tom
Casefold (convert uppercase to lowercase)
S = 'Tom 'print (s. casefold () # output: TOM
Center (specify the length and fill characters, center the content, and leave blank fill characters as spaces)
S = 'Tom 'print (s. center (20,'-') # output: -------- Tom ---------
Count (calculate the number of occurrences of a string. The second parameter is the start position and the third parameter is the end position)
S = 'aabbbcccdd' print (s. count ('CC', 3,11) # output: 2
Encode)
S = "Chinese" print (s. encode ('gbk') # output: B '\ xd6 \ xd0 \ xce \ xc4'
Endswith (judge whether the string ends with a certain character or string. The second parameter is the start position and the third parameter is the end position)
S = 'project' print (s. endswith ('ts') print (s. endswith ('e',) # output: True # True
Expandtabs (convert one tab key to seven spaces)
S = 'h \ Ti' print (s. expandtabs () # output: H I
Find (search for the index location of a character or string, the second parameter: start position, the third parameter: end position)
S = 'hello' print (s. find ('o') print (s. find ('O',) # returns-1 if not found # outputs: 4 #-1
Format (string formatting/splicing)
Name = 'Tom 'age = 18 s =' {0} \'s age is {1 }'. format (name, age) print (s) # or str = '{name} \'s age is {age}' result = str. format (age = 18, name = 'Tom ') print (result) # output: Tom's age is 18
Format_map
# Placeholder
Index (query the index location of a character or string, which is different from find. if the character does not exist, an error is returned)
S = 'hello' print (s. index ('o') print (s. index ('e',) # output: 4 #1
Isalnum (whether it is a letter or number)
S = '! # 'Print (s. isalnum () # output: False
Isalpha (letter or not)
S = '000000' print (s. isalpha () # output: False
Isdecimal (whether it is a decimal number)
S = '000000' print (s. isdecimal () # output: True # True: Unicode number, full-angle number (dual-byte) # False: Roman numerals, Chinese characters # Error: byte number (single-byte)
Isdigit (whether it is a number)
S = '000000' print (s. isdigit () # output: True # True: Unicode number, byte number (single-byte), full-byte number (double-byte), and Rome number # False: Chinese character number
Isidentifier (whether it is an identifier/variable name)
S = '1num' print (s. isidentifier () # output: False # because the variable name cannot start with a number
Islower (whether all are lowercase letters)
S = 'hello' print (s. islower () # output: False
Isnumeric (whether it is a number)
S = '000000' print (s. isnumeric () # output: True # True: Unicode number, full-angle number (dubyte), Roman number, Chinese character number
Isprintable (whether it is printable characters/whether it can be output as is)
S = '\ n' print (s. isprintable () # output: False
Isspace (whether it is a space)
Print (''. isspace () print ('\ t'. isspace () # output: True # True
Istitle (whether it is the title/uppercase of the first letter of each word)
Print ('Hello Boy '. istitle () print ('Hello boy'. istitle () # output: True # False
Isupper (whether all are uppercase letters)
Print ('Boy '. isupper () print ('Boy'. isupper () # output: True # False
Join (concatenates the elements in the sequence with specified characters to generate a new string)
S = ['H', 'e', 'L', 'L', 'O'] print (''. join (s) print ('-'. join (s) # output: Hello # H-e-l-o
Ljust (specify the length and fill characters. the content is left aligned. if the Fill character is left blank, it is a space)
S = 'hello' print (s. ljust (10, '-') # output: Hello -----
Lower (replace all strings with lowercase letters)
S = 'Tom 'print (s. lower () # output: TOM
Lstrip (remove the character specified on the left of the string. the default value is space)
S = 'Tom 'print (s. lstrip () # output: Tom
Maketrans (create a conversion table for character ING and use it with the translate function)
Intab = "abcde" outtab = "12345" trantab = str. maketrans (intab, outtab) str = "Hello abc" print (str. translate (trantab) # output: H5llo 123
Partition (specify a separator to separate strings)
S = 'amtom 'print (s. partition ('am') # output :(' I ', 'AM', 'Tom ')
Replace (replace old (old string) in the string with new (new string). If the third parameter max is specified, replace it with max no more than once .)
S = 'Tom 'print (s. replace ('M', 'o') # output: Too
Rfind (find the position where the specified string appears from the right side. If no match exists,-1 is returned)
S = 'one two one' print (s. rfind ('one') print (s. rfind ('one',) # specify the start position and end position # output: 8 #0
Rindex (locate the position where the specified string appears from the right side. If no matching item exists, an error is returned)
S = 'one two one' print (s. rindex ('one') print (s. rindex ('one',) # specify the start position and end position # output: 8 #0
Fill UST (specify the length and fill characters. the content is right aligned. if the Fill character is left blank, it is a space)
S = 'hello' print (s. rows UST (10, '-') # output: ----- Hello
Rpartition (specify the separator to split the string from the right)
S = 'iamtom _ IamTom 'print (s. rpartition ('am') # output :( 'iamtom _ I', 'AM', 'Tom ')
Rsplit (specify the delimiter to slice the string. if the second parameter num is specified, only num is separated and a list is returned)
S = 'a B c d' print (s. rsplit () print (s. rsplit ('', 2) # start from the right and separate them by spaces twice # output: ['A', 'B', 'C ', 'D'] # ['A B ', 'C', 'D']
Rstrip (delete the specified character at the end of the string, which is a space by default)
S = '!!! I am Tom !!! 'Print (s. rstrip ('! ') # Output :!!! I am Tom
Split (specify the delimiter to slice the string. if the second parameter num is specified, only num is separated and a list is returned)
S = 'a B c d' print (s. split () print (s. split ('', 2) # start from the left and separate them by space twice # output: ['A', 'B', 'C ', 'D'] # ['A', 'B', 'c d']
Splitlines (returns a list of strings separated by linefeeds)
S = 'a \ nb \ nc 'print (s. splitlines () # The default parameter is Falseprint (s. splitlines (True) # if the True parameter is specified, the linefeed is retained. # output: ['A', 'B', 'C'] # ['A \ n ', 'B \ n', 'C']
Startswith (determines whether the string starts with a certain character or string. The second parameter is the start position and the third parameter is the end position)
S = 'project' print (s. startswith ('prs') print (s. startswith ('e', 4, 8) # output: True # True
Strip (delete the specified character before and after the string, the default is space)
S = '!!! I am Tom !!! 'Print (s. strip ('! ') # Output: I am Tom
Swapcase (case-insensitive)
S = 'I am Tom' print (s. swapcase () # output: I AM tOM
Title (converted to the title, that is, the first letter of each word is capitalized)
S = 'I am tom' print (s. title () # output: I Am Tom
Translate (replace the characters in the table created based on the maketrans method)
Intab = "abcde" outtab = "12345" trantab = str. maketrans (intab, outtab) str = "Hello abc" print (str. translate (trantab) # output: H5llo 123
Upper (lowercase to uppercase)
S = 'hello' print (s. upper () # output: Hello
Zfill (specify the length of the string. Right alignment of the original string, with the front filled with 0)
S = 'hello' print (s. zfill (10) # output: 00000 Hello
The above is a detailed description of the python3 string function example. For more information, see other related articles in the first PHP community!