Python string formatting symbol

Source: Internet
Author: User

Python string formatting symbol

Format character conversion method
% C is converted to a character (ASCII value, or a string of one length)
% Ra converts strings with the repr () function first.
% S String Conversion using the str () function
% D/% I to a signed decimal number
% Ub to unsigned decimal number
% Ob to unsigned octal number
% Xb/% Xb (Unsigned) to Unsigned hexadecimal number (x/X indicates the size of the converted hexadecimal character
Lowercase)
% E/% E into scientific notation (e/E control output e/E)
% F/% F is converted to a floating point number (the fractional part is naturally truncated)
% G/% G % e and % f/% E and % F
% Output %

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `
Auxiliary instructions for formatting Operators
Symbol function
* Define the width or decimal point Precision
-Left alignment
+ Display the plus sign (+) before a positive number)
<Sp> A space is displayed before a positive number.
# Display zero ('0') before the octal value, and '0x 'or '0x' before the hexadecimal value (depending on
Use 'X' or 'X ')
Fill '0' in front of the number displayed by 0 instead of the default Space
% 'Output a single' %'
(Var) ing variable (Dictionary parameter)
M. n m is the minimum total width displayed, and n is the number of digits after the decimal point (if available)

The following are some examples of using format strings:
Hexadecimal output:
>>> "% X" % 108
'6c'
>>>
>>> "% X" % 108
'6c'
>>>
>>> "% # X" % 108
'0x6c'
>>>
>>> "% # X" % 108
'0x6c'
Floating Point Number and scientific notation form output:
>>>
>>> '% F' % 1234.567890
'1970. 123'
>>>
>>> '%. 2f' % 1234.567890
'1970. 57'
>>>
>>> '% E' % 1234.567890
'1. 234568E + 03'
>>>
>>> '% E' % 1234.567890
'1. 234568e + 03'
>>>
>>> '% G' % 1234.567890
'1970. 57'
>>>
>>> '% G' % 1234.567890
'1970. 57'
>>>
>>> "% E" % (111111111111111111l)
'1. 1111e + 21'
Integer and string output:
>>> "% + D" % 4
'+ 4'
>>>
>>> "% + D" %-4
'-4'
>>>
>>> "We are at % d %" % 100
'We are at 100'
>>>
>>> 'Your host is: % s' % 'Earth'
'Your host is: global'
>>>
>>> 'Host: % s/tPort: % d' % ('mars', 80)
'Host: mars Port: 80'
>>>
>>> Num = 123
>>> 'Dec: % d/oct: % # o/hex: % # x' % (num, num, num)
'Dec: 123/oct: 0173/hex: 0X7B'
>>>
>>> "MM/DD/YY = % 02d/% 02d/% d" % (2, 15, 67)
'Mm/DD/YY = 02/15/67'
>>>
>>> W, p = 'web', 'page'
>>> 'HTTP: // xxx. yyy. zzz/% s/%s.html '% (w, p)
'Http: // xxx. yyy. zzz/Web/page.html'

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> From string import Template
>>> S = Template ('there are $ {howation }$ {lang} Quotation symbols ')
>>>
>>> Print s. substitute (lang = 'python', howute = 3)
There are 3 Python Quotation Symbols

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.