This article introduces the methods of the pythonStr class.
# Capitalize (): The first character of the string, Capital string = 'This is a string. 'New _ str = string. capitalize () print (new_str) # output: This is a string. # center (width, fillchar = None): Place the string in the middle. enter string = 'This is a string at the beginning and end of the specified length. 'New _ str = string. center (30, '*') print (new_str) # output: ****** this is a string. * ***** # count (sub, start = None, end = None): calculates the number of characters in a string. string = 'This is a string. 'New _ str = string. count ('I') print (new_str) # output: 3 # d Ecode (encoding = None, errors = None): decodes string = 'This is a string. 'New _ str = string. decode () print (new_str) # encode (self, encoding = None, errors = None): encode string = 'This is a string. 'New _ str = string. encode () print (new_str) # endswith (self, suffix, start = None, end = None): determines whether to end with a character string = 'This is a string. 'New _ str = string. endswith ('ing. ') print (new_str) # output: Truenew_str = string. endswith ('XX') print (new_str) # Output: False # expandtabs (self, tabsize = None): returns a tab. Tabsize this option specifies the number of characters to replace with the tab "\ t'. the default value is 8string_expandtabs = 'This \ tis \ ta \ tstring. 'New _ str = string_expandtabs.expandtabs () print (new_str) # output: this is a string. # find (self, sub, start = None, end = None): find the position of the specified character in the string = 'This is a string. 'New _ str = string. find ('A') # Query result print (new_str) # output: 8new_str = string. find ('XX') # return-1 print (new_str) # output:-1 # format (* args, ** kwargs): Similar to % s usage, it implements string1 = 'My n through {} Ame is {0}, my job is {1 }. 'New _ str1 = string1.format ('yue', 'tester') print (new_str1) # output: My name is yue, my job is tester. string2 = 'My name is {name}, My job is {job }. 'New _ str2 = string2.format (name = 'yue', job = 'tester') print (new_str2) # output: 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') # Query result print (new_str) # output: 8new_s Tr = string. index ('XX') # print (new_str) # output: program running error, ValueError: substring not found # isalnum (self ): returns True if the string contains numbers and letters. otherwise, returns Falsestring = 'My name is yue, My age is 18. 'New _ str = string. isalnum () print (new_str) # output: Falsestring = 'haha18121314lala 'new _ str = string. isalnum () print (new_str) # output: True # isalpha (self): determines whether all strings are letters. If yes, True is returned, otherwise, Falsestring = 'abcdefg' new _ str = string. isalpha () Print (new_str) # output: Truestring = 'My name is yue 'new _ str = string. isalpha () # print (new_str) is not supported if there are spaces in the middle of the letter or special characters # output: False # isdigit (self): determines whether all strings are numbers. If yes, True is returned, otherwise, Falsestring = '000000' new _ str = string. isdigit () print (new_str) # output: Truestring = 'haha123lala 'new _ str = string. isdigit () # print (new_str) without spaces or special characters # output: False # islower (self): checks whether all letters in a string are in lowercase. If yes, True is returned, otherwise, Falsestring = 'My name is yue, my Ge is 18. 'New _ str = string. islower () print (new_str) # output: Truestring = 'My name is Yue, My age is 18. 'New _ str = string. islower () print (new_str) # output: False # isspace (self): checks whether all strings contain spaces. If yes, True is returned, otherwise, Falsestring = ''New _ str = string. isspace () print (new_str) # output: Truestring = 'My name is Yue, My age is 18. 'New _ str = string. isspace () print (new_str) # output: False # istitle (self): checks whether all characters in a string are spelled in uppercase and other letters are in lowercase. String = 'My Name Is Yue. 'New _ str = string. istitle () print (new_str) # output: Truestring = 'My name is Yue, My age is 18. 'New _ str = string. istitle () print (new_str) # output: False # isupper (self): checks whether all letters in a string are in uppercase. String = 'My name is yue. 'New _ str = string. isupper () print (new_str) # output: Truestring = 'My name is Yue. 'New _ str = string. isupper () print (new_str) # output: False # join (self, iterable): concatenates the elements in the sequence with specified characters to generate a new string. String = ("haha", "lala", "ohoh") str = "-" print (str. join (string) # output: haha-lala-ohoh # ljust (self, width, fillchar = None): returns the left alignment of an original string, and fill in the new string with spaces of the specified length. If the specified length is less than the length of the original string, the original string is returned. String = "My name is yue. "print (string. ljust (18) # output: 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): removes spaces or specified characters on the left of the string. String = "My Name is YUE. "print (string. lstrip () # output: My Name is YUE. string = "My Name is YUE. "print (string. lstrip ('My) # output: Name is YUE. # partition (self, sep): splits the string based on the specified delimiter. String =" http://www.mingyuanyun . Com "print (string. partition (': //') # output :( 'http ',': // ', 'www .mingyuanyun.com') # replace (self, old, new, count = None): replace the old string with new (new string). If the third parameter max is specified, it cannot exceed max. String = "My name is yue. "print (string. replace ("yue", "ying") # output: My name is ying. # rfind (self, sub, start = None, end = None): returns the position of the last occurrence of a string. If no match exists,-1 is returned. String = "My name is yue. "print (string. rfind ('is') # output: 8 string = "My name is yue. "print (string. rfind ('XXX') # output:-1 # rindex (self, sub, start = None, end = None): returns the position of the substring str in the string, if no matching string exists, an exception is reported. you can specify the optional parameter [beg: end] to set the search interval. String = "My name is yue. "print (string. rindex ('is') # output: 8 string = "My name is yue. "print (string. rindex ('XXX') # output: ValueError: substring not found # Convert UST (self, width, fillchar = None): returns the right alignment of an original string, and fill in the new string with spaces to the length width. If the specified length is less than the length of the string, the original string is returned. String = "My name is yue. "print (string. must UST (18) # output: My name is yue. # rpartition (self, sep): splits the string from the right to the specified separator. String =" http://www.mingyuanyun . Com "print (string. rpartition ('.') # output :(' http://www.mingyuanyun ','. ', 'Com') # split (self, sep = None, maxsplit = None): slices strings by specifying separators. String = "haha lala gege" print (string. split ('') # output: ['hahaha', 'Lala ', 'gege'] print (string. split ('', 1) # output: ['hahaha', 'Lala gege'] # rsplit (self, sep = None, maxsplit = None ): slice the string from the right by specifying a separator. String = "haha lala gege" print (string. rsplit ('') # output: ['hahaha', 'Lala ', 'gege'] print (string. rsplit ('', 1) # output: ['Haha lala ', 'gege'] # rstrip (self, chars = None ): deletes the specified character (space by default) at the end of a string ). string = "My name is yue. "print (string. rstrip () # output: My name is yue. # strip (self, chars = None): removes the character specified at the beginning and end of the string (space by default ). String = "My name is yue. "print (string. strip () # output: My name is yue. # splitlines (self, keepends = False): returns a list containing rows as elements, separated by rows ('\ r',' \ r \ n', \ n, if the keepends parameter is False, it does not contain line breaks. if it is True, the line breaks are retained. 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 ): check whether the string starts with the specified sub-string. If yes, True is returned. otherwise, False is returned. If the beg and end parameters are specified, check within the specified range. String = "My name is yue. "print (string. startswith ('my') # output: Truestring = "My name is yue. "print (string. startswith ('yue') # output: False # swapcase (self): converts uppercase and lowercase letters of a string. String = "My Name Is Yue. "print (string. swapcase () # output: mY nAME iS yUE. # title (self): returns a "title" string, that is, all words start with an uppercase letter, and other letters are lowercase letters (see istitle ()). String = "my name is yue, my age is 18. "print (string. title () # output: My Name Is Yue, My Age Is 18. # translate (self, table, deletechars = None): convert string characters based on the table (containing 256 characters) given by the parameter table, and put the characters 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. translate (trantab) # output: my n 1M3 4S y53 # upper (self): converts lowercase letters in a string into uppercase letters. String = "my name is yue, my age is 18. "print (string. upper () # output: my name is yue, my age is 18. # zfill (self, width): returns a string of the specified length. the original string is right-aligned and filled with 0 in front. String = "my name is yue." print (string. zfill (18) # output: 000my name is yue.
The above is the details about the methods of the python Str class. For more information, see other related articles in the first PHP community!