Python, pythonformat

Source: Internet
Author: User

Python, pythonformat

First, we will introduce the format function in python. Next, we will introduce the simple use of the python. format () function.

--- Restore content start ---

The format function in python is used for string formatting.

Keyword

Print ('{name} today {action }'. format (name = 'chen moumou ', Action = 'shoot video') # Use the keyword grade = {'name': 'chen moumou', 'fenshu ': '59'} print ('{name} electrician tested {fenshu }'. format (** grade) # using keywords, you can use a dictionary to add ** to the dictionary when a value is input.

Pass location

Print ('{1} today {0 }'. format ('shoot video', 'chen moumou ') # print by position (' {0} today {1 }'. format ('chen moumou ', 'shoot video '))

Fill and alignment ^ <> indicates center, left, right, and rear with width.

Print ('{: ^ 14 }'. format ('chen moumou ') print (' {:> 14 }'. format ('chen moumou ') print (' {:< 14 }'. format ('chen moumou ') print (' {: * <14 }'. format ('chen moumou ') print (' {:&> 14 }'. format ('chen moumou ') # Fill and align ^ <> center, left, right, and back with width

Precision and Type f accuracy are often used with f

print('{:.1f}'.format(4.234324525254))print('{:.4f}'.format(4.1))

Hexadecimal conversion. B o d x represents binary, octal, decimal, and hexadecimal respectively.

Print ('{: B }'. format (250) print ('{: o }'. format (250) print ('{: d }'. format (250) print ('{: x }'. format (250) delimiter, which is only applicable to print ('{:,}'. format (100000000) print ('{:,}'. formats (235445.234235 ))

The following describes the format function in python:

Used to format strings

1. Pass by location

Print ('{0} xxxxxxxx {1}'. format ('this is what 0 is going to pass in ', 'this is what 1 wants to pass in '))

2. pass through keywords

 print('{0}xxxxxxxx{1}'.format(0='xxx',1='xxx'))

For dictionary

Print ('{keys} xxxxxxxx {values}'. format (** <dictionary Name> ))

3. Act as the thousands separator for numbers (for example, 1,000,000)

 print('{:,}'.format(1000000))

Summary

The above is a simple tutorial on using the format () function in python. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.