String: Formatting

Source: Internet
Author: User

The Format method for string formatting

The format method accepts positional parameters and keyword parameters

#位置参数 >>> "{0} love {1}". Format (' I ', ' I ') ' I love you ' #关键字参数 >>> ' {A} love {b} '. Format (a= ' i ', b= ') ' I Love you ' #位置参数和关键词参数可以混用, but the positional parameter must precede the keyword parameter >>> "{0} love {b}". Format (' i ', b= ' i ') ' I Love You ' >>> "{a {0} ". Format (a= ' I ', ' you ') syntaxerror:positional argument follows keyword argument
>>> ' {0:.1f}{1} '. Format (27.658, ' GB ') ' 27.7GB ' #位置参数中加冒号, which means that the beginning of a formatted symbol #.1 means to round up and keep a decimal; F means to print out a fixed-point number # (This part is not understood for the time being)

Second, the string dedicated operator%.

The function of #用法如下 >>> '%c '%100    #%c is to format the character and its Acⅱ code, Acⅱ code in 100 for ' d ' d ' >>> ' I love%s '% ' you '     #实际意思就是把 ' to %s form output ' I love you ' >>> '%s%s '% (' a ', ' B ', ' C ')    #有多个字符串时需要用元组表示 ' a b C ' >>> '%d +%d =%d '% (1,2,1+2)    ' 1 + 2 = 3 '

  

String formatting symbolic meaning

character Number
say Ming
%c
formatting characters and their ASCII Code
%s
Formatting Strings
%d
formatting integers
%o
formatting an unsigned octal number
%x
formatting unsigned hexadecimal numbers
%x
format unsigned hexadecimal number (uppercase)
%f
format fixed-point number to specify the precision after the decimal point
%e
format with scientific notation fixed-point number
%E
function with %e , formatted with scientific notation fixed-point number
%g depending on the size of the value, use %f Live %e
%G
function with %g , depending on the size of the value, use %f or %E



Formatting operator Auxiliary directives

character Number
speak clearly
M.N
m is the minimum total width of the display, N is the number of digits after the decimal point
     -
use to align left
      +
show plus sign (+) in front of positive number
     #
display '0o 'before octal number, '0x ' or '0X ' before hexadecimal number
0
The displayed number is preceded by a fill of '0 ' instead of a space




String escape character meaning

character Number
speak clearly
\ '
single quotation marks
     \"
Double quotes
\a
issue system ring tones
\b
Backspace
\ n
line break
\ t
Horizontal tab (tab)
\v
Portrait tab
\ r
Carriage return character
\f
page Break
\o
characters represented by octal numbers
\x
The hexadecimal number represents the character
/
represents a null character
     \\
back slash

String: Formatting

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.