String common functions:
- remove whitespace
- strip() #remove all whitespace
- lstrip() #remove left blank
- rstrip() #remove right blank
- Split
- s.split("sep")) #Split by sep, return a list, excluding sep
- s.rsplit("sep") #Find from the right
- s.partition (self,sep) #Separate by sep, divide s into three parts: head, sep, tail
- s.rpartition (self,sep) #Find from the right
- splitlines(self,keepends =None) #Returns a list containing each line as an element. If the parameter keepends is False, it does not contain newlines. If it is True, newlines are preserved.
- length
s.len() - index
s[1] - Slice
s[::-1] #Display in reverse order - Increase
#Operate on the sequence (use ' ' and ':' as separators respectively) >>> seq1 = ['hello','good','boy','doiido'] >>> print ' '.join(seq1) hello good boy doiido >>> print ':'.join(seq1) hello:good:boy:doiido # operate on strings >>> seq2 = "hello good boy doiido" >>> print ':'.join(seq2) h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o # operate on the tuple >>> seq3 = ('hello','good','boy','doiido') >>> print ':'.join(seq3) hello:good:boy:doiido # operate on the dictionary >>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4} >>> print ':'.join(seq4) boy:good:doiido:hello #Merge directories >>> import os >>> os.path.join('/hello/','good/boy/','doiido') '/hello/good/boy/doiido'
- find
- s.find() #Find character does not exist, return -1
- s.index() #The search character does not exist, and an error is reported directly
- s.rfind() #Find from the right
- s.rindex() #Find from the right
- Replace
s.replace("Character to be replaced", "Character to replace") - Encoding and decoding
s.encode(encoding = "utf-8") encoding
s.decode(") decoding - Count str times from a range
s.count(self, sub, start =None, end =None) - what starts and ends with
- s.startswith(self,suffix,start =None,end =None) #Whether (in the specified range) starts with suffix, it returns True
- s.endwith(self,suffix,start =None,end =None) #Whether (in the specified range) ends with suffix, it returns True
- Convert the \t contained in the string into tabsize spaces
s.expandtabs(self, tabsize=8) - formatted output
- print('{0}{1}{0}'.fo rmat('a','b'))
- print('{}{}{ }'.format('a','b'))
- print('{name} {age}'.format(age=12,name='lhf'))
S.format(*args, **kwargs)
- Whether to judge the class
- isinstance(a,typ) #Determine whether the variable a is the type represented by typ, if it is, return True
- s.isalnum(self) or s.isalpha(self) #At least one character, and all letters or numbers return True
- s.isdecimal(self) #If there are only decimal characters in s, return True, otherwise error.
-
s.isdigit(self) #Whether all numbers are
-
s.isidentifier(self) #The string returns True as a keyword
-
s.islower() #At least one character, and all lowercase letters will return True
- s.isnumeric(self) #If there are only numeric characters in S, return True,
- s.issprintable() #The characters are printable and empty, then return false, such as "\n" and so on return false
- s.isspace() # At least one character, and all spaces return True (at least one space)
- s.istitle() #Whether the first letter is capitalized (title format)
- s.isupper() #Whether it is uppercase
- align
- s.ljust(length, padding) # Left-aligned , can be filled with fixed-length characters
- s.rjust(length, padding) #Right alignment , can pad fixed-length characters
- s.center(self,width,fiilchar =None) #Align in the play, can fill fixed-length characters
- s.zfiil(width) #The original character is right-aligned, not enough to be filled with 0
- case conversion
- s.capitalize() #The first letter is capitalized, and the rest of the letters are all lowercase
- s.lower() #Get a copy of all letters converted to lowercase
- s.upper() #Get a copy of all letters converted to uppercase
- s.swapcase() #case inversion
- s.title() #Capitalize the first letter of each word (title format) and lowercase the rest of the letters
- plaintext conversion
- maketrans(self, *args, **kwargs) set the correspondence table
- s.translate(self,table)
- str_in = "abcdef"
- str_out = "123456"
- table = str.maketrans(str_in,str_out) >>>{97: 49, 98: 50, 99: 51, 100: 52, 101: 53, 102: 54} #correspondence table
- s = "hello world"
- s.translate(table) >>>s = "h5llo worl4"