Python2.7-string Module

Source: Internet
Author: User
Tags iterable

String module

1. Constants defined within the module

Letters (ascii_letters, letters), numbers (digits, hexdigits, octdigits), spaces (whitespace), capitalization (Ascii_lowercase, Ascii_uppercase, lowercase, uppercase), punctuation (punctuation), the printable collection in the foregoing (printable)

2, formatted string, basically similar to the built-in STR type function, the format is "{" [Field_name] ["!" conversion] [":" Format_spec] "}"
Field_name is the name of the label at the format, you can omit
Conversion can be value R or S, respectively, represented by REPR () and STR ()
Format_spec format is [[Fill]align][sign][#][0][width][,][.precision][type]
Fill is any character that fills to the width length
Align can be "<", ">", "=", "^", respectively, left-aligned, right-aligned, and filled between the numbers ' symbols and numbers, centered
Sign can be "+", "-", "", respectively, to add symbols for all numbers, add symbols only for negative numbers, add spaces before positive numbers, and add symbols for negative numbers
# add "0b", "0o", "0x" respectively before binary 86 or 16
0 The effect is the same as [fill] set to "0" or [align] set to "="
width Specifies the length of the string
, add commas to each three digits
. precision specifies the number of decimal digits, cannot be used for integers, and specifies the maximum number of characters for non-numeric use
Type can be "B" (binary), "C" (numeric to Unicode character), "D" (decimal), "E" (scientific notation), "E", "F" (Default 6 decimal places), "F", "G" (common format), "G", "N" (with G), "O" (octal), "s" (default format, String), "X" (hexadecimal), "x", "%" (the number is represented by percentile)

3. Template class in the string module

With $ ID, similar to% and formatted
>>> from string import Template
>>> s = Template (' $who likes $what ')
>>> S.substitute (who= ' Tim ', what= ' Kung Pao ')
' Tim likes Kung Pao '

4. Other methods of the string module

String.capwords (s [, Sep]) is similar to Str.title (), except that the SEP split string can be set
String.maketrans (from, to) is the same as str.translate ()

Summarize:
The function of the string module is essentially the same as the built-in STR type

Add some of the commonly used STR functions:
Str.count (Sub [, Start[,end]]) Statistics the number of sub occurrences in Str
Str.endswith (suffix [, start[,end]]) determines whether the end of STR is suffix
Str.startswith (prefix [, start[,end]]) to determine whether STR starts with prefix
Str.format (*args, **keargs) formatting strings
Str.strip ([chars]) removes the specified character from the front and back of STR, the default is a space, and the Lstrip and Rstrip methods
Str.split ([Sep[,maxsplit]]) returns the list of split strings with Sep, with a maximum of maxsplit times
Str.join (iterable) connects the Iterable object of the string type to str.
Str.upper (). Lower () to case the string

Python2.7-string Module

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.