Formatted output of Python

Source: Internet
Author: User

One.

% form

A string that begins with a percent sign as a placeholder, substituting the syntax for a character after the string, and the column: "Today's weather is%s"% "Sunny". Note that there is a space before and after the%, and when there are multiple needs to be replaced, the strings used for substitution need to be enclosed in parentheses and separated by commas.

In addition, the placeholder has multiple forms,%d is for the int type, and%s is a string, but the integer and float types are also available, and%f is used for floating-point types, which can be controlled in the form of%.3f, where the number 3 represents the retention of three decimal places.

* If you want to output% in the print with the% form placeholder, you need to add a%, that is: percent to normal display%.

Two.

Format form

Unlike the% form, this form uses {} as a placeholder, in addition, the% form is a fixed syntax, and format is a function of the string in Python, so its usage is different, namely: "Today's weather is {}". Format ("Sunny"). When there are multiple substitutions, the strings used for substitution need to be enclosed in parentheses and separated by commas.

  

# formatted Output S12 = "I'm%s, I'm%d years old, I like%s"% (' Sylar ', 18, ' Jay ') # before the wording print (s12) S12 = "I'm {}, this year {} years old, I like {}". Format ("Jay Chou", 28, " Chow Yun-Fat ") # Format print by location (s12) S12 =" I call {0}, this year {2} years old, I like {1} ". Format (" Jay Chou "," Chow Yun-Fat ", 28) # Specify the location of print (s12) S12 =" I'm {name}, this year {age} " , I like {singer} ". Format (name=" Jay Chou ", singer=" Chow Yun-Fat ", age=28) # Specify Keywords

  

Formatted output of Python

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.