Example analysis of Str.format () usage of Python development

Source: Internet
Author: User
In this paper, the Str.format () usage of Python development is analyzed. Share to everyone for your reference, as follows:

Format the output of a string, we can see in many places, such as: C + + has seen

Here's a look at the string Format function Str.format () in Python:

#使用str. Format () function # use ' {} ' placeholder print (' I\ ' m {},{} '. Format (' Hongten ', ' Welcome to my space! ') Print (' # ') #也可以使用 ' {0} ', ' {1} ' placeholder print (' {0},i\ ' m {1},my e-mail is {2} '. Format (' Hello ', ' hongten ', ' Hongtenzone@foxmail.com ') #可以改变占位符的位置print (' {1},i\ ' m {0},my e-mail is {2} '. Format (' Hongten ', ' Hello ', ' Hongtenzone@foxmail.com ') print (' # ') #使用 placeholder print (' Hi,{name},{message} ' in the form of ' {name} '. Format (name = ' Tom ', message = ' How old is? ') Print (' # ') #混合使用 ' {0} ', ' {name} ' form print (' {0},i\ ' m {1},{message} '. Format (' Hello ', ' hongten ', message = ' This is a test message! ')) Print (' # ') #下面进行格式控制import mathprint (' The value of PI is approximately {}. '). Format (Math.PI)) print (' The value of pi is approximately {!r} '. Format (Math.PI)) print (' The value of pi is approximately {0:.3f} '. Format (MATH.PI)) Table = {' Sjoerd ': 4127, ' Jack ': 4098, ' dcab ': 7678}for name, phone in Table.items (): Print (' {0:10} ==&gt ; {1:10d} '. Format (name, phone)) Table = {' Sjoerd ': 4127, ' Jack ': 4098, ' Dcab ': 8637678}print (' jack: {0[jack]:d}; SjOerd: {0[sjoerd]:d}; ' Dcab: {0[dcab]:d} '. Format (table)

Operating effect:

Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> i ' m hongten,welcome to my space!####################################### #Hello, I ' m Hongten,my e-mail Is Hongtenzone@foxmail.comhello,i ' m hongten,my e-mail is hongtenzone@foxmail.com################################### # # # # # #Hi, tom,how old was you?####################################### #Hello, I ' m hongten,this is a test message!######### ############################## #The value of pi is approximately 3.141592653589793.The value of pi is approximately 3.14159 2653589793.The value of PI is approximately 3.142.Jack    ==>    4098Sjoerd   ==>    4127Dcab    ==>    

I hope this article is helpful for Python program design.

  • 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.