Analysis of str. format () usage examples in python development

Source: Internet
Author: User
This article mainly introduces str in python development. format () usage, combined with the instance form for a more detailed analysis of str. the format () function features, usage and related precautions. the code contains detailed annotations. if you need this function, you can refer to the example in this article to analyze the str of python development. format () usage. We will share this with you for your reference. The details are as follows:

You can see the output result of formatting a string in many places, such as c/c ++.

Let's take a look at the string format function str. format () in python ():

# Use str. format () function # Use '{}' placeholder print ('I \' m {},{}'. format ('hongten ', 'Welcome to my space! ') Print (' # '* 40) # You can also use the placeholder print (' {0} ',' {1} 'in the form of' {0 }, I \'m {1}, my E-mail is {2 }'. format ('hello', 'hongten ', 'hongtenzone @ foxmail.com') # you can change the placeholder position print ('{1}, I \' m {0 }, my E-mail is {2 }'. format ('hongten ', 'hello', 'hongtenzone @ foxmail.com') print ('#' * 40) # Use the placeholder print ('Hi, {name}, {message }'. format (name = 'Tom ', message = 'How old are you? ') Print (' # '* 40) # Use' {0} 'in combination,' {name} 'in print (' {0 }, I \'m {1}, {message }'. format ('hello', 'hongten ', message = 'This is a test message! ') Print (' # '* 40) # 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 }=>{ 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 ))

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 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 are 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.141592653589793.The value of PI is approximately 3.142.Jack    ==>    4098Sjoerd   ==>    4127Dcab    ==>    7678Jack: 4098; Sjoerd: 4127; Dcab: 8637678>>> 

I hope this article will help you with Python programming.

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.