Python standard library notes (1)-string module, pythonstring

Source: Internet
Author: User

Python standard library notes (1)-string module, pythonstring

The String module contains a large number of practical constants and classes, as well as outdated legacy functions, and can also be used as a String operation.

1. Common Methods
Common Methods Description
Str. capitalize () Uppercase letters
Str. center (width) Fill the original string with spaces into a string with width, and the original string content is centered
Str. count (s) Returns the number of times the string s appears in str.
Str. decode (encoding = 'utf-8', errors = 'strict ') Decodes a string in a specified encoding format.
Str. encode (encoding = 'utf-8', errors = 'strict ') Encode a string in a specified encoding format
Str. endswith (s) Judge whether str ends with string s
Str. find (s) Returns the position index of string s in string str. If no value exists, returns-1.
Str. index (s) Similar to the find () method, but if s does not exist in str, an exception is thrown.
Str. isalnum () Returns True if str contains at least one character and both letters and numbers; otherwise, returns False.
Str. isalpha () Returns True if str contains at least one character and all letters; otherwise, returns False.
Str. isdigit () Returns True if str only contains numbers; otherwise, returns False.
Str. islower () True is returned if str is case-sensitive and all characters are lowercase. Otherwise, False is returned.
Str. isspace () True is returned if str contains only spaces. Otherwise, False is returned.
Str. istitle () True is returned if str is title-based (the first letter of a word is capitalized); otherwise, False is returned.
Str. isupper () True is returned if str is case sensitive and all characters are uppercase. Otherwise, False is returned.
Str. ljust (width) Returns a new string with the left alignment of the original string and filled with spaces to the width.
Str. lower () Converts all uppercase characters in str to lowercase.
Str. lstrip () Remove the invisible characters on the left of str
Str. partition (s) Split str into three values by s
Str. replace (a, B) Replace a in string str with B
Str. rfind (s) Similar to the find () function, but the query starts from the right side.
Str. rindex (s) Similar to index (), but starting from the right
Str. Fill ust (width) Returns a new string with the right alignment of the original string and filled with spaces to the width.
Str. rpartition (s) Similar to the partition () function, but the query starts from the right side.
Str. rstrip () Remove the invisible characters on the Right of str
Str. split (s) Split str with s as the Separator
Str. splitlines () Returns a list containing rows as elements.
Str. startswith (s) Check whether the str string starts with s. If yes, True is returned. Otherwise, False is returned.
Str. strip () Equivalent to executing both rstrip () and lstrip ()
Str. title () Returns the title str. All words start with uppercase and all other letters are lowercase.
Str. upper () Returns an uppercase string of all str characters.
Str. zfill (width) Returns a string with the length of width. The original string str is right-aligned and filled with 0 in front.
2. string constants
Constant Description
String. ascii_lowercase Lowercase letter 'abcdefghijklmnopqrstuvwxy'
String. ascii_uppercase Uppercase letter 'abcdefghjklmnopqrstuvwxy'
String. ascii_letters Connection string of ascii_lowercase and ascii_uppercase Constants
String. digits String ranging from 0 to 9: '123'
String. hexdigits String '0123456789abcdefabcdef'
String. letters String 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy'
String. lowercase Lowercase letter string 'abcdefghijklmnopqrstuvwxy'
String. octdigits String '20140901'
String. punctuation All punctuation characters
String. printable A printable character string. Contains numbers, letters, punctuation marks, and spaces
String. uppercase University letter string 'abcdefghijklmnopqrstuvwxy'
String. whitespace Blank characters '\ t \ n \ x0b \ x0c \ R'
3. String Template

You can use string. Template to customize string replacement standards for Python. The following is a specific column:

>>> From string import Template >>> s = Template ('$ who like $ what') >>> print s. substitute (who = 'I', what = 'python') I like python >>> print s. safe_substitute (who = 'I') # I like $ what> Template ('$ {who} LikePython') is not thrown when the key is missing '). substitute (who = 'I') # Use {} 'ilikepython 'in the string'

Template also has more advanced usage. You can customize templates of different forms by inheriting string. Template, rewriting variables delimiter (delimiter) and idpattern (replacement format.

Import stringtemplate_text = ''' Delimiter: $ de Replaced: % with_underscore Ingored: % notunderscored ''d = {'de': 'Not replicased', 'with _ underscore ': 'replicased', 'notunderscored': 'Not replicased'} class MyTemplate (string. template): # The delimiter of the rewrite Template is "%". The replacement mode (idpattern) must contain underscores (_) delimiter = '%' idpattern = '[a-z] + _ [a-z] + 'print string. template (template_text ). safe_substitute (d) # use the original Template to render print MyTemplate (template_text ). safe_substitute (d) # Use the rewritten MyTemplate for rendering

Output:

    Delimiter : not replaced    Replaced : %with_underscore    Ingored : %notunderscored    Delimiter : $de    Replaced : replaced    Ingored : %notunderscored

It can be seen that the native Template only renders the definition operator $. After rewriting, MyTemplate renders the definition operator % and replaces the format with underlines.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.