Python Basic Tutorial Chapter 3rd--String

Source: Internet
Author: User

1. String formatting

String formatting operator%+ conversion flag + Minimum field width + dot followed precision value + conversion type

  

The string module provides another way to format the from string import Template s=template (' $x. Glorious $x! ') S.substitute (x= ' Slim ')

Example: '%03.4f '% pi ' 3.1416 '

    Prevent "label table" before field width and precision value The table can be 0, plus, minus, or space

Example: '%012.4f '% pi

' 0000003.1416 '

2. String method

2.1 String Constants

String.dights number 0-9

String.letters All Letters

String.lowercase All lowercase Letters

string.printable all printable characters

String.punctuation Strings for all punctuation

String.upercase All uppercase letters

2.2 String method

The Find method can find substrings in a longer string. Returns the leftmost index of the location of the substring, otherwise returns-1

Find can provide a starting and ending point, eg. Subject.find ("111", 0,16)

The Join method is used to add elements to the queue, which must be a string

Lower the lowercase master that returns a string

The Replace method is used to return a string after all occurrences of a string have been replaced

Split is used to split a string into a sequence, which is your method of join, and if you do not provide a delimiter, the program will use all the spaces as separators (spaces, tabulation, line wrapping, etc.)

The Strip method returns a string that strips both sides (excluding interior) spaces, or specifies the characters that need to be removed, and lists them as arguments

The Translate method can replace some parts of a string, but translate simply removes the single character. The advantage is that multiple substitutions can be made at the same time, sometimes more efficient than replace

Before the translate conversion, you need to complete a conversion table, using the From string import Maketrans, Maketrans has 2 parameters: two equal-length strings, representing each word in the 1th string nonalphanumeric the same position in the 2nd string substitution. The second parameter of the translate is optional and is used to specify the character to be deleted

  

  

  

  

  

Python Basic Tutorial Chapter 3rd--String

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.