Python Summary 3. python Summary

Source: Internet
Author: User

Python Summary 3. python Summary

Elegant string

Preface

I remember that I mentioned the character string in the first article of the Python study summary. That is to say hello first, because I didn't mention any string processing methods. Today, I would like to explain in detail how to use strings in Python. If there are any improper or deficiencies, please point out and add them. Thank you first.

Basic string operations

What can we immediately think of in sequence operations? Index? Slice? Multiplication? Judge membership? Length? What is the maximum and minimum? Yes, we can also use it in the field of string. But keep in mind that strings are immutable. Therefore, it is impossible to implement multipart assignment in strings.

String formatting

When it comes to string formatting, it is very common. How do we use it in C ++? Memset? In Python, the character string formatting operator is expressed by the percent sign (%.

Note: % can also be used as a modulo operation.

String formatting method: % s is used for all the characters to be formatted. Then, put a string (the string to be formatted) on the left side of %, and put the value to be used to format the string on the right side (it can be a number or a string, it can also be a tuple or dictionary that contains multiple values ).

Note: The number of % s and the value used to format the string must be the same.

The % s part of the formatted string is called the conversion specifier, marking the location where the conversion value needs to be inserted. If you want to format a real number (floating point number), you can use the f specifier and provide the required precision: A period plus the number of decimal places you want to retain (which will be explained later ).

Template string: format the string by calling the template method in the string. Specific Method: the substitute template method replaces $ foo in the string with the passed keyword parameter foo.

 

The running result is as follows:

Basic conversion instructions:

(1) % character: mark the start of the conversion specifier.

(2) Conversion flag (optional):-indicates left alignment (right alignment by default), + indicates adding a plus or minus sign before the conversion value, 0 indicates that the conversion value is filled with 0 if the number of digits is not enough.

(3) Minimum field width after % (optional): The converted string must have at least the width specified by this value.

(4) Point (.) followed by the precision value (optional): If the conversion is a real number, the precision value indicates the number of digits after the decimal point. If the conversion is a string, the number indicates the maximum field width. If it is *, the precision will be read from the tuples.

 

 

String Method

Find: search for a substring in a long string. It returns the leftmost index of the substring location. If not found,-1 is returned.

Join: add an element to the queue.

Lower: returns the lowercase letter version of the string.

Title/capwords: converts a string into a title (that is, the first letter of all words in upper case and the other letters in lower case)

Replace: returns the string obtained after all matching items of the string are replaced.

Split: it is the inverse method of join. It splits strings into sequences. (If NO separator is provided, all spaces are used as separators by default)

Strip: returns a string that removes spaces on both sides (excluding internal spaces. You can also specify characters on both sides to be removed and column them as parameters.

Lstrip and rstrip.

Translate: like the replace method, some parts of the string can be replaced, but unlike the former, the translate method only processes a single character. Its advantage is that it can be replaced at the same time. Before using the translate conversion, you need to complete a conversion table in advance (You guessed it). We can use the maketrans function in the string module.

The maketrans function accepts two parameters: two equal-length strings, indicating that each character in the first string is replaced by a character in the same position in the second string.

Related Article

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.