Introduction to Python str class methods
#capitalize (): string first character uppercase String = ' This is a string. ' New_str = String.capitalize () print (NEW_STR) #输出: The is a string. #center (width, fillchar=none): Place the string in the middle, and at the specified length, fill in string = ' This is a string with the specified character. ' New_str = String.center (30, ' * ') Print (NEW_STR) #输出: ******this is a string.******* #count (sub, Start=none, End=none): Counts the number of characters in a string = ' This is a string .' New_str = String.count (' i ') print (NEW_STR) #输出: 3 #decode (Encoding=none, Errors=none): Decodes string = ' This is a string. ' New_ str = String.decode () print (NEW_STR) #encode (self, Encoding=none, Errors=none): encoded string = ' This is a string. ' New_str = str Ing.encode () print (NEW_STR) #endswith (self, suffix, start=none, end=none): Determines whether a character ends with string = ' This is a string. ' New_str = String.endswith (' ing. ') Print (NEW_STR) #输出: truenew_str = String.endswith (' xx ') print (NEW_STR) #输出: False #expandtabs (Self, Tabsize=none): Returns a tab character. Tabsize This option specifies the number of characters to be replaced with the tab "\ T", which defaults to 8string_expandtabs = ' this\tis\ta\tstring. ' new_str = String_expandtabs.expandtabs () Print (NEW_STR) #输出: This is A string. #find (self, sub, Start=none, End=none): Finds the position of the specified character in a string, String = ' This is a string. ' New_str = String.find (' a ') #找的到的情况prin T (NEW_STR) #输出: 8new_str = String.find (' xx ') #找不到的情况返回 -1print (new_str) #输出: -1 #format (*args, **kwargs): Similar to the use of%s, it through {} To implement string1 = ' My name is ' {0},my job ' is {1}. ' NEW_STR1 = String1.format (' Yue ', ' tester ') print (NEW_STR1) #输出: My name is yue,my job was tester.string2 = ' My name is ' {name},m Y job is {job}. ' NEW_STR2 = String2.format (name= ' Yue ', job= ' tester ') print (NEW_STR2) #输出: My name is yue,my job is tester. #index (self, sub, Start=none, End=none): similar to findstring = ' This is a string. ' New_str = String.index (' a ') #找的到的情况print (new _STR) #输出: 8new_str = String.index (' xx ') #找不到的情况, program error print (NEW_STR) #输出: program operation error, valueerror:substring not found #isalnum ( Self): Determines whether the string is all numbers and letters, or returns TRUE if it is, otherwise returns falsestring = ' My name is yue,my 18. ' New_str = String.isalnum () print (NEW_STR) #输出: falsestring = ' Haha18121314lala ' new_str = String.isalnum () print (NEW_STR) #输出: True #isalPHA (self): Determines whether the string is all letters, or returns TRUE if it is, otherwise returns falsestring = ' ABCDEFG ' new_str = String.isalpha () print (NEW_STR) # Output: truestring = ' My name is yue ' new_str = String.isalpha () #字母中间带空格, no special characters print (NEW_STR) #输出: False # isdigit (self): judgment word String, if True, returns falsestring = ' 1234567890 ' new_str = String.isdigit () print (NEW_STR) #输出: truestring = ' Haha123lala ' new_str = String.isdigit () #中间带空格, no special characters print (NEW_STR) #输出: False # islower (self): Determines whether the letters in a string are lowercase, Returns true if it is, otherwise returns falsestring = ' My name is Yue,my 18. ' New_str = String.islower () print (NEW_STR) #输出: truestring = ' My name is Yue,my ' 18. ' New_str = String.islower () print (NEW_STR) #输出: False # isspace (self): Determines whether the string is a space, returns true if it is, or returns falsestring = ' new_s TR = String.isspace () print (NEW_STR) #输出: truestring = ' My name is Yue,my age is 18. ' New_str = String.isspace () print (NEW_STR) #输出: False # Istitle (self): detects if all words in the string are spelled in uppercase, and the other letters are lowercase. string = ' My name is Yue. ' New_str = String.istitle () print (NEW_STR) #输出: truestring = ' My name is Yue,my ageis 18. ' New_str = String.istitle () print (NEW_STR) #输出: False # Isupper (self): detects whether all letters in the string are uppercase. string = ' My name is YUE. ' New_str = String.isupper () print (NEW_STR) #输出: truestring = ' My name is YUE. ' New_str = String.isupp ER () print (NEW_STR) #输出: False # join (self, iterable): Generates a new string for the element in the sequence with the specified character connection. String = ("haha", "lala", "Ohoh") str = "-" Print (Str.join (string)) #输出: Haha-lala-ohoh # Ljust (self, width, fillchar=none) : Returns the left alignment of an original string and fills a new string of the specified length with a space. Returns the original string if the specified length is less than the length of the original string. string = "My name is Yue." Print (String.ljust) #输出: My name is Yue. # lower (self): Converts all uppercase characters in a string to lowercase. String = "My Name is YUE." Print (String.Lower ()) # output: My name is Yue. # Lstrip (self, chars=none): Truncates the left space of the string or specifies the character. String = "My Name is YUE." Print (String.lstrip ()) #输出: My name is yue.string = "My name is YUE." Print (String.lstrip (' My)) #输出: Name is YUE. # partition (self, SEP): Splits the string according to the specified delimiter. string = "http://www.mingyuanyun.com" Print (String.partition ('://')) #输出: (' http ', '://', ' www.mingyuanyun.com ') # Replace (self, old, new, count=nOne): Replace the old (older string) in the string with the new one, and if you specify the third parameter max, the replacement does not exceed Max times. string = "My name is Yue." Print (String.Replace ("Yue", "Ying")) #输出: My name is Ying. # RFind (self, sub, Start=none, End=none): Returns the position of the last occurrence of the string, or 1 if there is no match. string = "My name is Yue." Print (String.rfind (' is ')) #输出: 8string = "My name is Yue." Print (String.rfind (' XXX ')) #输出: -1 # Rindex (self, sub, Start=none, End=none): Returns the position of the substring str in the last occurrence of the string, if there is no matching string to report the exception, You can specify an optional parameter [Beg:end] to set the search interval. string = "My name is Yue." Print (String.rindex (' is ')) #输出: 8string = "My name is Yue." Print (String.rindex (' XXX ')) #输出: valueerror:substring not Found # Rjust (self, Width, fillchar=none): Returns the right alignment of an original string, and fills the new string with the width of length with a space. Returns the original string if the specified length is less than the length of the string. string = "My name is Yue." Print (String.rjust) #输出: My name is Yue. # Rpartition (Self, SEP): Splits the string from right to the specified delimiter. string = "http://www.mingyuanyun.com" Print (String.rpartition ('. ')) #输出: (' Http://www.mingyuanyun ', '. ', ' com ') # split (self, Sep=none, Maxsplit=none): Slices A string by specifying a delimiter. string = "haha lala gege" Print (STRING.SPLIt (")) #输出: [' haha ', ' lala ', ' Gege ']print (String.Split (', 1)) #输出: [' haha ', ' lala Gege '] # rsplit (self, sep=none, MAXSP Lit=none): Slices the string from right by specifying a delimiter. string = "haha lala gege" Print (String.rsplit (")) #输出: [' haha ', ' lala ', ' Gege ']print (String.rsplit (", 1)) #输出: [' Haha lal A ', ' Gege '] # Rstrip (self, chars=none): Deletes the specified character at the end of the string (the default is a space). String = "My name is Yue. Print (String.rstrip ()) #输出: My name is Yue. # strip (Self, chars=none): Removes the character specified by the tail of the string (the default is a space). string = "My name is Yue." Print (String.strip ()) #输出: My name is Yue. # Splitlines (self, keepends=false): Separated by line (' \ r ', ' \ r \ n ', \ n '), returns a list containing the rows as elements, if the argument keepends is False, does not contain a newline character, if true, The line break is preserved. str1 = ' ab c\n\nde fg\rkl\r\n ' Print (Str1.splitlines ()) # output: [' ab C ', ', ' de fg ', ' kl ']STR2 = ' ab c\n\nde fg\rkl\r\n ' Print ( Str2.splitlines (True)) # output: [' ab c\n ', ' \ n ', ' de fg\r ', ' kl\r\n '] # startswith (self, prefix, Start=none, end=none): Checks if the string Is the beginning of the specified substring, or True if it is, or False if it is returned. If the parameter beg and end specify a value, the check is within the specified range. string = "My name IS Yue. Print (String.startswith (' My)) #输出: truestring = "My name is Yue." Print (String.startswith (' Yue ')) #输出: False # swapcase (self): Converts the uppercase and lowercase letters of a string. String = "My Name is Yue." Print (String.swapcase ()) #输出: MY NAME is YUE. # title: Returns a "header" string, meaning that all words start with uppercase and the remaining letters are lowercase (see istitle ()). string = "My name is Yue,my 18." Print (String.title ()) #输出: My Name is yue,my 18. # Translate (self, table, Deletechars=none): Converts the character of a string according to the table given by the parameter table (containing 256 characters), and the character to be filtered out into the del parameter. From string Import maketransstr = "AOEIU" num = "12345" Trantab = Maketrans (str, num) string = "My name is Yue" Print (string.t Ranslate (trantab) # output: My n1m3 4s y53 # upper (self): Converts lowercase letters in a string to uppercase. string = "My name is Yue,my 18." Print (String.upper ()) #输出: MY NAME is yue,my 18. # Zfill (self, width): Returns a string of the specified length, the original string is right-aligned, and the front padding is 0. string = "My name is Yue." Print (String.zfill) #输出: 000my name is Yue.