There are a variety of string manipulation functions in Python. In history, the string class has experienced a history of samsara in Python. At the very beginning, Python had a dedicated string module that would use string to import first, but later, because of the numerous suggestions from Python users, starting with python2.0, the string method was changed to S.method () Call, as long as S is a string object that can be used without import. At the same time, in order to maintain backward compatibility, a string module is still retained in Python, where the method defined is the same as S.method (), and all of these methods point to a function called with S.method (). It is important to note that S.method () can invoke more methods than string, such as IsDigit (), Istitle (), and so on, only in the form of S.method ().
For a String object, the first thought might be to calculate how many characters it has, it is easy to think of using S.len (), but this is wrong, it should be Len (S). Because Len () is a built-in function, included in the __builtin__ module. Python does not include Len () in the string type, at first glance it seems a bit incomprehensible, in fact, everything has its logical logic inside. Len () can calculate not only the number of characters in a string, but also the number of members of a list, the number of members of a tuple, and so on, so it is not appropriate to count Len () in a string, so it is possible to use Len () as a general function to implement different types of operations with overloading. There is also the ability to include a Len () function in each type that has a Len () operation. Python chooses the first solution. Similar to the STR (ARG) function, it is represented by the string type Arg.
Character-case transformations in strings:
- S.lower () #小写
- S.upper () #大写
- S.swapcase () #大小写互换
- S.capitalize () #首字母大写
- String.capwords (S)
#这是模块中的方法. It separates S with the split () function, then uses capitalize () to capitalize the initial letter and merge it with join ().
- S.title () #只有首字母大写, the remainder is lowercase, this method is not in the module
The alignment of the string at output:
- S.ljust (Width,[fillchar])
#输出width个字符, S left-aligned, insufficient parts are filled with Fillchar, the default is a space.
- S.rjust (Width,[fillchar]) #右对齐
- S.center (width, [Fillchar]) #中间对齐
- S.zfill (width) #把S变成width长, and right-aligned, less part with 0 complement
Search and Replace in a string:
- S.find (substr, [Start, [end]])
#返回S中出现substr的第一个字母的标号, returns-1 if there is no substr in S. Start and End function is equivalent to searching in s[start:end]
- S.index (substr, [Start, [end]])
#与find () is the same, but when there is no substr in S, a run-time error is returned
- S.rfind (substr, [Start, [end]])
#返回S中最后出现的substr的第一个字母的标号, if there is no substr in S, then return-1, that is, the first occurrence of substr from the right of the first letter marking
- S.rindex (substr, [Start, [end]])
- S.count (substr, [Start, [end]]) #计算substr在S中出现的次数
- S.replace (Oldstr, Newstr, [Count])
#把S中的oldstar替换为newstr, Count is the number of replacements. This is a common form of substitution, and there are some functions for replacing special characters
- S.strip ([chars])
#把S中前后chars中有的字符全部去掉, it can be understood to replace s before and after chars to none
- S.lstrip ([chars])
- S.rstrip ([chars])
- S.expandtabs ([tabsize])
#把S中的tab字符替换没空格, each tab is replaced by a tabsize space, which defaults to 8
Splitting and grouping of strings:
- S.split ([Sep, [Maxsplit]])
#以sep为分隔符, divide s into a list. Maxsplit represents the number of splits. The default delimiter is a blank character
- S.rsplit ([Sep, [Maxsplit]])
- S.splitlines ([keepends])
#把S按照行分割符分为一个list, Keepends is a bool value that retains the row delimiter after each row is true.
- S.join (seq) #把seq代表的序列-string sequence, connected by s
The mapping of the string, which contains two functions:
- String.maketrans (from, to)
#返回一个256个字符组成的翻译表, where the characters from the from are converted to by one by one, so the from and to must be equal in length.
- S.translate (Table[,deletechars])
#使用上面的函数产后的翻译表, translate s and delete the characters in the Deletechars. It is important to note that if S is a Unicode string, then the Deletechars parameter is not supported, and the same functionality can be achieved by translating a character to none. You can also use the functionality of the codecs module to create more powerful translation tables.
The string also has a pair of encoded and decoded functions:
- S.encode ([encoding,[errors]])
#其中encoding可以有多种值, such as gb2312 GBK gb18030 bz2 zlib big5 bzse64, are supported. The default value for errors is "strict", which means unicodeerror. Possible values are ' ignore ', ' replace ', ' xmlcharrefreplace ', ' backslashreplace ' and all the values registered by Codecs.register_error. This section covers the codecs module, which is not a specific understanding
- S.decode ([encoding,[errors]])
Test functions for strings, which are not in the string module and return bool values:
- S.startwith (Prefix[,start[,end])
#是否以prefix开头
- S.endwith (Suffix[,start[,end])
#以suffix结尾
- S.isalnum ()
#是否全是字母和数字, and has at least one character
- S.isalpha () #是否全是字母, and has at least one character
- S.isdigit () #是否全是数字, and has at least one character
- S.isspace () #是否全是空白字符, and has at least one character
- S.islower () #S中的字母是否全是小写
- S.isupper () #S中的字母是否便是大写
- S.istitle () #S是否是首字母大写的
String type conversion functions, which are only available in the string module:
- String.atoi (S[,base])
#base默认为10, if 0, then s can be 012 or 0x23 This form of string, if it is 16 then s can only be 0x23 or 0x12 this form of string
- String.atol (S[,base]) #转成long
- String.atof (S[,base]) #转成float
Once again, the string object is immutable, meaning that you cannot change a part of the character after Python creates a string. Any of the above functions will return a new string after the string has been changed, and the original is not changed. In fact, this is also a workaround, you can use S=list (s) This function to change S into a single character as a member of the list, so you can use the s[3]= ' a ' method of changing the value, and then use s= "". Join (s) revert to a string
Some methods of Python str