In this blog, "Python string series," The following is covered:
- Python built-in STR objects and operations
- Formatting of strings
- Unicode strings in Python
- Regular expressions in Python
- Re module
This article describes Python's built-in STR types, and enumerates the methods supported by string objects in Python, which enable powerful string processing capabilities.
In Python 2, a plain string is clearly distinguished from a Unicode string, both of which are basic types built into Python, such as:
>>> type (str) <type ' type ' >>>> type (Unicode) <type ' type ' >
A string of type str is commonly referred to as a normal string (plain string), which differs from a Unicode type string (Unicode string):
>>> s = ' This is a plain string. ' >>> type (s) <type ' str ' >>>> u = U ' This is a Unicode string. ' >>> type (u) <type ' Unicode ' >
Unicode strings usually start with ' u '.
In Python 2, use the abstract base class basestring to determine whether a string is an instance of the STR type or Unicode type, because both are subclasses of the basestring.
>>> issubclass (str, basestring) true>>> Issubclass (Unicode, basestring) true>>> Issubclass (Unicode, str) False
This article describes the built-in methods for Python strings, which also apply to plain string and Unicode strings, and if the operation's S is a plain string, the returned string is also a plain string,unicode similar. Subsequent articles will analyze in detail the characteristics of Unicode strings and some of the characteristics of coding.
In Python, strings are immutable sequences that support operations such as repeating, linking, indexing, and slicing, for example:
>>> S * N # will s repeat N times >>> s1 + s2 # string S1 and S2 connection >>> s[0] # index string S1 first character >> ;> S[0:2] # Returns the first two characters in a string s
None of these actions will change the string that participates in the operation unless you explicitly assign the value. In addition, Python includes a number of tips for string handling, such as:
- Use s[::-1] to flip the entire string
- If a string consists entirely of numbers and begins with 0, use INT (s) to automatically remove the beginning of 0 and convert the original string to a meaningful integer.
Python has a rich string processing feature built in
1. Capitalize first letter
Capitalize ()
S.capitalize ()
Returns a copy of s that does not change S. If the first character of S is a letter, the first letter of the copy is converted to uppercase, and all the remaining letters are converted to lowercase.
For example:
>>> ' This is a test string. '. Capitalize () ' This is a test string. ' >>> ' _this is a test string. '. Capitalize () # does not start with a letter, not a change ' _this is a test string. ' >>> ' This is A test string. '. Capitalize () # All the letters in the position except the opening are turned into lowercase ' This is a test string. '
2. Alignment
(1) Align Right and left Ljust (),rjust()
S.ljust (width[, Fillchar])
S.rjust (width[, Fillchar])
Returns a string of length Max (Len (s), width), if width > len (s), aligns left/right, and fills the other end with Fillchar
For example:
>>> ' 1234 '. Rjust (8, ' # ') ' # # #1234 ' >>> ' 1234 '. Ljust (8, ' # ') ' 1234#### ' >>> ' 1234 '. Ljust (2, ' # ') ' 1234 '
(2) Center Center ()
S.center (n, fillchar= ")
Returns a new string, the length of the new string is Max (Len (s), N), and when n > Len (s), the remainder of the new string is populated with the parameter Fillchar (the default is a space), and S is placed in the middle of the new string.
For example:
>>> ' Test '. Center (3) ' Test ' >>> ' Test '. Center (5) ' Test ' >>> ' Test '. Center (6, ' # ') ' #test # ' >>> ' Test ' center (7, ' ~ ') ' ~~test~ '
Visible when the left and right cannot be evenly populated, the first padding is filled.
3. Counting
Count ()
S.count (Sub, start=0, End=sys.maxint)
Count S[start:end], the number of times a sub-string sub appears.
4. The conversion of STR to Unicode
(1) str to Unicode--decode ()
S.decode ([encoding[,errors]])
Use the Decode () function to convert a plain string of type str to a string of Unicode type.
For example:
>>> s = ' Hello ' >>> u = S.decode (' GBK ') >>> type (s) <type ' str ' >>>> type (u) <type ' Unicode ' >>>> print s Hello >>> print u hello >>> s ' \xc4\xe3\xba\xc3 ' >>> uu ' \u4f60\ U597d ' >>> len (s) 4>>> len (u) 2
(2) Unicode to Str--encode ()
S.encode ([encoding[,errors]])
Use Encode () to convert a Unicode string to a plain string of type Str.
For example:
>>> u = U ' Hello ' >>> s = U.encode (' GBK ') >>> type (u) <type ' Unicode ' >>>> type (s) <type ' str ' >>>> uu ' \u4f60\u597d ' >>> s ' \xc4\xe3\xba\xc3 ' >>> print u hello >>> Print s Hello >>> len (u) 2>>> len (s) 4
5. Front and rear prefixes
EndsWith (),startswith()
S.endswith (suffix[, start[, end]])
S.startswith (prefix[, start[, end]])
Returns the bool type result,
Determines whether s[start:end] ends with suffix.
6. Extended tab
Expandtabs ()
S.expandtabs ([tabsize])
Tabsize defaults to 8, which returns a copy of S, where the "\ T" is extended to tabsize spaces.
For example:
>>> ' Test\ttest '. Expandtabs () ' Test test '
7. Positioning
(1) Return-1 find when not in position (),rfind()
S.find (Sub [, Start [, end]])
S.rfind (Sub [, Start [, end]])
Returns an int that represents the first subscript in sub-s[start:end]. Returns 1 if no sub-string sub is found in S[start:end].
For example:
>>> ' Testtest '. Find (' est ') 1>>> ' testtest '. Find (' TT ') 3>>> ' testtest '. Find (' TT ', 3) 3> >> ' Testtest '. Find (' TT ', 4)-1
(2) Throw exception when not positioned Index (),rindex()
S.index (Sub [, Start [, end]])
S.rindex (Sub [, Start [, end]])
The function is similar to find (), but throws ValueError if no sub is found.
For example:
>>> ' Hello this World '. Index (' $ ') Traceback (most recent call last): File "<stdin>", line 1, in <modu Le>valueerror:substring not found
8.
Format ()
S.format (*args, **kwargs)
Returns a string result,
9. Content form Judgment
isalnum (),isalpha(),isdigit(),isspace()
S.isalnum ()
Returns a Boolean result,
Determines if S is non-empty and consists entirely of letters and numbers .
S.isalpha ()
Returns a Boolean result,
Determines if S is non-empty and consists entirely of letters .
S.isdigit ()
Returns a Boolean result,
Determines if s is non-null and is all composed of numeric characters.
For example:
>>> ' 123 '. IsDigit () true>>> ' 123.456 '. IsDigit () False
S.isspace ()
Returns True if Len (s) > 0, and all characters in it are spaces;
Returns False if S is null, or if there is at least one non-whitespace character in S.
10. Case
(1) Lowercase islower (),Lower()
S.islower ()
S.lower ()
Returns a Boolean result,
If s does not contain a lowercase letter, or at least one uppercase letter, it returns false, otherwise true, including other characters, is not affected.
For example:
>>> ' 123.456 '. Islower () false>>> ' ABCDE '. Islower () true>>> ' abcde$ '. Islower () true> >> ' abcde#%^% '. Islower () true>>> ' AbcdeF '. Islower () false>>> ' a.213214$#@^%[email protected] '. Islower () True
(2) Uppercase Isupper (),Upper()
S.isupper ()
S.upper ()
Returns True if all letters contained in s are uppercase
Returns False if S does not contain a letter, or contains at least one lowercase letter.
Cases:
>>> ' [email protected] '. Isupper () true>>> ' Asdfgq '. Isupper () false>>> '. Isupper () False
(3) Swap case swapcase ()
S.swapcase ()
One. "titlecase"
Istitle (),title()
S.istitle ()
S.title ()
Determine if a string is "titlecase": Each individual contiguous letter segment begins with an uppercase letter.
For example:
>>> ' a title '. Istitle () true>>> ' a title '. Istitle () false>>> ' 123 This is A string '. Istitle () False>>> ' This is a String '. Istitle () False
Connection
Join ()
S.join (iterable)
Concatenate strings in iterable with S as delimiters
For example:
>>> ' $ '. Join ([' Hello ', ' this ', ' World ']) ' Hello$this$world '
Split
(1) One-time split partition for reserved separators (),rpartition()
S.partition (Sep)
S.rpartition (Sep)
Split s with Sep , return (head, Sep, tail) in the form of triples.
For example:
>>> ' Hello$this$world '. Partition (' $ ') (' Hello ', ' $ ', ' This$world ') >>> ' Hello$this$world '. Rpartition (' $ ') (' Hello$this ', ' $ ', ' world ')
>>> ' Hello this world '. Partition (' $ ')
(', ', ', ' Hello This World ')
(2) full split split with no delimiter left (),rsplit(),splitlines()
S.split ([chars])
S.rsplit ([Sep [, Maxsplit]])
S.splitlines (Keepends=false)
For example:
>>> ' Hello$this$world '. Split (' $ ') [' Hello ', ' this ', ' world ']
14.
Lstrip (), Strip (),rstrip()
S.lstrip ([chars]) #从开头删除s. Strip ([chars]) # Left and right both ends delete S.rstrip ([chars]) # Delete from the end
16. Replace
Replace ()
S.replace (old, new[, Count])
18.
Translate ()
S.translate (table [, Deletechars])
.
Zfill()
S.zfill (width)
(original) Python string series (1)--str object