Home > Backend Development > Python Tutorial > Detailed explanation of str operation in Python

Detailed explanation of str operation in Python

PHP中文网
Release: 2017-06-21 17:21:36
Original
1289 people have browsed it

1. str.format(): Use "{}" placeholder to format the string (the index number form and key-value pair form in the placeholder can be mixed).

 1 >>> string = 'python{}, django{}, tornado{}'.format(2.7, 'web', 'tornado')  # 有多少个{}占位符就有多少个值与其对应,按照顺序“填”进字符串中 2 >>> string 3 'python2.7, djangoweb, tornadotornado' 4 >>> string = 'python{}, django{}, tornado{}'.format(2.7, 'web') 5 Traceback (most recent call last): 6   File "<pyshell#6>", line 1, in <module> 7     string = 'python{}, django{}, tornado{}'.format(2.7, 'web') 8 IndexError: tuple index out of range 9 >>> string = 'python{0}, django{2}, tornado{1}'.format(2.7, 'web', 'tornado')  # 也可以指定“填”进去的值(从0开始,后面的值不一定都要用上,但是要保证指定的位置是有值的)10 >>> string11 'python2.7, djangotornado, tornadoweb'12 >>> string = 'python{py}, django{dja}, tornado{tor}'.format(tor='tornado', dja='web', py=2.7)  # 可以使用键值对的形式赋值13 >>> string14 'python2.7, djangoweb, tornadotornado'15 >>>
Copy after login

2. Use “%” for string formatting.

# Python automatically determines and converts to %E or %F based on the size of the number##%%
Format symbol table
%c Convert to single character
%r Convert to a string expressed using repr()
%s Convert to a string expressed using str()
%d or %i Convert to signed decimal integer
%u Convert to unsigned Convert the decimal integer
%o to an unsigned octal integer
%x It is an unsigned hexadecimal integer, and hexadecimal letters are expressed in lowercase.
%X is converted into an unsigned hexadecimal integer, hexadecimal The hexadecimal letters are expressed in uppercase letters
%e and converted into floating point numbers expressed in scientific notation, where the e is expressed in lowercase letters
%E is converted to a floating point number expressed in scientific notation, in which E is expressed in uppercase letters
%f or #F is converted to Floating point number
%g is automatically determined by Python and converted to %e or %f
%G## based on the size of the number.
Output "%"

Auxiliary formatting symbol table*-+# before the hexadecimal number. ##0When the size of the number is less than the requirement of m.n, fill it with 0m.nm is displayed The minimum total width of , n is the number of digits after the decimal point (if available)
Define width or decimal point precision
Left-aligned
Output the positive value symbol "+" for positive numbers
The size of the number is insufficient When m.n is required, use spaces to pad
# to display 0 before the octal number and 0x or 0X

The above is the detailed content of Detailed explanation of str operation in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template