Python: String Manipulation Summary

Source: Internet
Author: User

All standard sequence Operations (index, Shard, multiply, Judge membership, length, minimum maximum) also apply to strings, and the strings are immutable.

One, string format conversion specifier[Note]: The order of these items is critical

(1)% character : The beginning of the token conversion specifier

(2) Conversion flag (optional)

turn Change flag action
- Left justified
+ plus sign before converting values
"" (blank string) positive number leave space
0 The number of converted value bits is insufficient 0 padding

(3) Minimum field width (optional): The converted string should have at least the width specified by the value. [Note]: if *, the width is read from the value tuple

(4) point (.) Heel precision value (optional):

A, if the conversion is real, the precision value represents the number of digits after the decimal point

b, if the string is converted, the precision value represents the maximum field width

C, if it is *, then the precision value will be read from the tuple

(5) Conversion type [note]: Red is commonly used

Conversion type Meaning
D,i Signed decimal integer
O octal integers without symbols
U Non-signed decimal integer
X unsigned hexadecimal integer (lowercase)
X unsigned hexadecimal integer (uppercase)
E Floating-point number represented by scientific notation (lowercase)
E Floating-point number represented by scientific notation (uppercase)
F,f Decimal floating-point number
G If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
G If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
C Single-character (accepts integers or single character strings)
R String (any Python object converted using repr)
S String (any Python object converted using str)

Examples:

1pi=3.14159262 Print('%10f'%PI)#width is ten3 Print('%10.2f'%PI)#width is 10 and accuracy is 24 Print('%+10.2f'%PI)#width is 10, accuracy is 2, home plus minus sign before digit5 Print('%0+10.2f'%PI)#The width is 10, the precision is 2, the number is home plus minus, and is filled with 0 instead of a space.6 Print('%-+10.2f'%PI)#width is 10, precision is 2, home plus minus sign before digit, and left justified

1Name='tomwenxing'2 Print('%20s'%name)#width is3 Print('%20.3s'%name)#width is 20 and accuracy is 34 Print('%-20.3s'%name)#width is 20, precision is 3, left justified

1 name='tomwenxing'2print('%-*.*s'  # use * As field width or precision

[Note]: The formatted string of the dictionary

After the% character in each conversion specifier, you can add a key (enclosed in parentheses), followed by another description element. However, when using a dictionary in this way, any number of conversion specifiers can be used as long as all the given key values can be found in the dictionary.

1phonebook={'Beth':'9012','Alice':'2341','Cecil':'3258'}2Message="Cecil ' s phone number is% (Cecil) s"%Phonebook3 Print(message)

Python: String Manipulation Summary

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.