Transfer from:
string in python have a variety of operating functions. In the history of string classes in python, has gone through a cycle of history. At the very beginning, python has a special string of the module, to use string method first import, but as many of the recommendations of python users, starting from the python2.0, string method to use S.method () in the form of calls, as long as S is a string object can be used without import. Meanwhile, in order to maintain backward compatibility, the current python still retains a string of the module, which defines the method and S.method () is the same, these methods are the final point of using S.method () function to call. pay attention to, S.method () method can call the module than in the multi-string, such as isdigit (), istitle (), etc. can only be used S.method () calls the way.
on A string object, the first thought is to calculate its operation may the number of characters, very easy to use S.len (), but this is wrong, it should be len (S). as len () is the built-in function , including __builtin__ module. python not to len () included in the string type, seems a bit incomprehensible at first glance, in fact, everything has a rational logic inside. len () can be calculated not only the characters in the string number, you can also calculate the number of members list, tuple number of members, etc., so only the len () operator in the string is not appropriate, so one can len () as a general function with different types of heavy achieve operation, there is in each can have len () operation type must contain a len () function. python choice is the first solution. There are similar str (arg) function, which the arg be expressed with the string type.
case characters in the string transformation:
S.lower () # lowercase
S.upper () # upper
S.swapcase () # case each for
S.capitalize () # first letter
String.capwords (S)
# This is the module approach. it the S with the split () function separately, and then capitalize () the first uppercase letters, and finally join () merged together
S.title () # only the first letter capitalized, the rest is lowercase, the module is not the way
alignment in the output string:
S.ljust (width, [fillchar])
# output width characters, S left justified, and the gap filled with fillchar, the default is blank.
S.rjust (width, [fillchar]) # right-aligned
S.center (width, [fillchar]) # Center Align
S.zfill (width) # width into the length of S, and in the right alignment, make up the shortfall with 0 < br> string search and replace:
S.find (substr, [start, [end]])
# S appears substr returns the first letter of the label, if the S did not return substr role-1.start and end equivalent to the S [start: end] in the search
S.index (substr, [start, [end]])
# and find () the same, but in S, not substr, it will return a runtime error
S.rfind (substr, [start, [end]])
# returns the last occurrence of substr S in the first letter of the label, if there is no substr S returns -1, that is counting from the right, the first occurrence of substr in the first letter of the label
S.rindex (substr, [start, [end]])
S.count (substr, [start , [end]]) # calculate substr number of occurrences in S
S.replace (oldstr, newstr, [count])
# S in oldstar to replace newstr, count the number for the replacement. This is Replace the generic form, there are some special characters function replacement
S.strip ([chars])
# S in the chars before and after the character in some all removed, can be understood as the chars before and after replacing S To None
S.lstrip ([chars])
S.rstrip ([chars])
S.expandtabs ([tabsize])
# tab characters in the S did not replace the spaces, Each tab is replaced tabsize spaces, the default is 8
string segmentation and combination:
S.split ([sep, [maxsplit]])
# to sep as the delimiter, the S split into a number of list.maxsplit said. The default separator is whitespace
S.rsplit ([sep, [maxsplit]])
S.splitlines ([keepends])
# to S in accordance with the line separator into a list, keepends is a bool value, if true will be retained after each line and the line separator.
S.join (seq) # seq represent the sequence of string sequence of connection with the S up
string mapping, this function contains two functions:
String.maketrans (from, to)
# returns a 256-character translation table in which the characters are one by one from converted to the corresponding to, from and to it must be of equal length.
No comments:
Post a Comment