Learn to write: Python formatting operators

Source: Internet
Author: User

1. Introduction

(1)%s takes precedence with the STR () function, but can be followed by a list, a number, a string

"%s%s"% ("name", "age") displays multiple characters

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/84/8D/wKiom1eUcDazqp0fAAAdfW1eHz4527.png "title=" Sogou 20160724153634.png "alt=" Wkiom1eucdazqp0faaadfw1ehz4527.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/84/87/wKiom1eTI-TzbmpdAAAfUB1Mp7c544.png "title=" Sogou 20160723155914.png "alt=" Wkiom1eti-tzbmpdaaafub1mp7c544.png "/>

(2)%c converted to character

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/84/87/wKioL1eTIwDzjyqWAAAIuEWSBIQ799.png "title=" Sogou 20160723155456.png "alt=" Wkiol1etiwdzjyqwaaaiuewsbiq799.png "/>

(3)%d or%i, converted to integers

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/84/8D/wKioL1eUcPLASBHXAAAQYku2NHM708.png "title=" Sogou 20160724154001.png "alt=" Wkiol1eucplasbhxaaaqyku2nhm708.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/84/87/wKioL1eTJTywHlWPAAAxAAK_3xc863.png "title=" Sogou 20160723160426.png "alt=" Wkiol1etjtywhlwpaaaxaak_3xc863.png "/>

(4)%o converted to unsigned 8 or 16, if the plus # sign is signed

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/84/87/wKiom1eTJlWy9I6TAAANjnJHMIs818.png "title=" Sogou 20160723160733.png "alt=" Wkiom1etjlwy9i6taaanjnjhmis818.png "/>

(5) Conversion of%e or%e to scientific counting method

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/84/87/wKiom1eTJ9yiEmRlAAAHbNmDlTU973.png "title=" Sogou 20160723161614.png "alt=" Wkiom1etj9yiemrlaaahbnmdltu973.png "/>

(6)%f or%f converted to floating-point numbers, you can add x.y x in front of F to represent the total number of digits, and Y for the right of the decimal point

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/84/87/wKioL1eTJ_qQ4Z8LAAAMxRrwICc214.png "title=" Sogou 20160723161645.png "alt=" Wkiol1etj_qq4z8laaamxrrwicc214.png "/>

2. Script instance

User input content, printed on the screen, such as

#!/usr/bin/env python

width = 48
Content = []

While True:
line = Raw_input ("Enter line (. To quit) > ")
if line = = ".":
Break
Content.append (line)

Print "+%s+"% ("*" * width)
For line in content:
Sp_wid, extra = Divmod (Width-len (line)), 2)
Print "+%s%s%s+"% (' * sp_wid, line, ' * (Sp_wid + extra))
Print "+%s+"% ("*" * width)

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/84/8F/wKioL1eUvJ2Bfw5HAAAhnp4xiu0681.png "title=" Sogou 20160724210302.png "alt=" Wkiol1euvj2bfw5haaahnp4xiu0681.png "/>

Actually the string comes with the center function, above just to consolidate the knowledge points

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/84/8F/wKioL1eUvTvCtLqDAAAIW41YvMQ816.png "title=" Sogou 20160724210453.png "alt=" Wkiol1euvtvctlqdaaaiw41yvmq816.png "/>


This article is from the "Court of the Odd Tree" blog, please be sure to keep this source http://zhangdl.blog.51cto.com/11050780/1829443

Learn to write: Python formatting operators

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.