Methods for printing and formatting strings in Python

Source: Internet
Author: User

Original

Convert a value to a string

Python has several ways to convert any value to a string: pass it to repr() or str() function.

The difference between repr () and STR (), see a few examples:

>>> Print (str (' 123 '))       123                         >>> print (str (123))         123                         >>> Print (repr (' 123 ') ))      ' 123 '                       >>> print (repr (123))        123

Look at this example again.

>>> from datetime import datetime>>> today = DateTime.Now () >>> print (str (now)) 2017-04-22 15:41:33.012917>>> Print (repr (now)) Datetime.datetime (2017, 4, 22, 15, 41, 33, 12917)

So str() repr() The difference is that:

    • str()The output of the pursuit of readability, the output format is easy to understand, suitable for output content to the user terminal.
    • repr()The output of the pursuit of clarity, in addition to object content, but also need to show the object's data type information, suitable for the development and debugging phase of use.

Use of Str.format

The parentheses and the characters inside them (called Format field) are format() replaced by the parameters in. The number in parentheses is used to point to the position of the incoming object in format() .

>>> print (' {0} and {1} '. Format (' spam ', ' eggs ')) spam and eggs>>> print (' {1} and {0} '. Format (' spam ', ' Eggs ')) eggs and spam

If format() keyword parameters are used in, their values point to parameters that use that name.

>>> print (' This {food} is {adjective}. '. Format (       ..... food= ' spam ', adjective= ' absolutely horrible ') this spam is absolutely horrible.

Optional ‘:‘ and format identifiers can be followed by field name. This allows for better formatting of values. The following example retains the Pi to three digits after the decimal point.

>>> Import math>>> Print (' The value of PI is approximately {0:.3f} '. Format (MATH.PI)) The value of pi is approximately 3.142.

‘!a‘(use ascii() ), ‘!s‘ (Use str() ) and ‘!r‘ (used repr() ) can be used to convert a value before it is formatted:

>>> Import math>>> Print (' The value of PI is approximately {}. '). Format (MATH.PI)) The value of pi is approximately 3.14159265359.>>> print (' The value of pi is approximately {!r}. ' . Format (Math.PI)) The value of pi is approximately 3.141592653589793.

Format dictionary, using * *

>>> table = {' Sjoerd ': 4127, ' Jack ': 4098, ' dcab ': 8637678}>>> print (' Jack: {jack:d}; Sjoerd: {sjoerd:d}; Dcab: {dcab:d} '. Format (**table)) jack:4098; sjoerd:4127; dcab:8637678

Legacy String Formatting

Use% for string formatting

>>> Import math>>> Print (' The value of pi is approximately%5.3f. '% Math.PI) The value of pi is Approxim Ately 3.142.

Methods for printing and formatting strings in Python

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.