Python full stack _python string formatting

Source: Internet
Author: User

1. Use placeholders to reserve locations for real values and specify the format
Print ("I ' m%s.") I ' m%d year old "% (' AAA ', 18))

We can also use dictionaries to pass real values. As follows:

Common format characters

%s: string

%d: decimal integer
%b: Binary integer
%x: Hexadecimal integer
%e: Index base is E

%E: Base is E

%f: Floating point

2. Use Format ()

Reference http://www.cnblogs.com/hongten/archive/2013/07/27/hongten_python_format.html

Use the ' {} ' placeholder
Print (' I\ ' m {},{} '. Format (' Hongten ', ' Welcome to my space! ')

You can also use ' {0} ', a placeholder in the form of ' {1} '
Print (' {0},i\ ' m {1},my e-mail is {2} '. Format (' Hello ', ' hongten ', ' [email protected] ')
You can change the position of the placeholder
Print (' {1},i\ ' m {0},my e-mail is {2} '. Format (' Hongten ', ' Hello ', ' [email protected] ')

Placeholder using the form ' {name} '
Print (' Hi,{name},{message} '. Format (name = ' Tom ', message = ' How old is You? '))

Mixed with ' {0} ', ' {name} ' form
Print (' {0},i\ ' m {1},{message} '. Format (' Hello ', ' hongten ', message = ' This is a test message! ')

Python full stack _python string formatting

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.