Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use print () to format output in Python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use print () formatted output in Python, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

The formatter reserves the location for the real value and controls the format of the display. Formatters can contain a type code that controls the type of display, as follows:

% s string (with display of str ())

% r string (with display of repr ())

% c single character

% b binary integer

% d decimal integer

% I decimal integer

% o Octal integer

% x hexadecimal integer

% e index (base is written as e)

% E Index (base is written as E)

% f floating point number

% F floating point number, same as above

% g index (e) or floating point number (depending on display length)

% G Index (E) or floating point number (depending on display length)

"% character"% "

You can further control the format in the following ways:

% [flags] [width]. [precision] typecode

Flags can have +, -,''or 0. + indicates right alignment. -indicates left alignment.' 'is a space, which means to fill a space to the left of a positive number to align with a negative number. 0 means to fill with 0.

Width represents the display width

Precision represents the precision after the decimal point

Typecode represents the above d, f, s such as d, f, s

For example:

Print ("% + 10x" 10)

Print ("d" 5)

Print ('6.3f' 2.3)

The width and precision above are two integers. We can use * to dynamically replace these two quantities. For example:

Print ("%. * f"% (4,1.2))

Python actually replaces * with 4. So the actual template is ".4f".

Specific operations:

The% operator built into Python can be used to format string operations and control the presentation format of strings. There are other ways to format strings in Python, but the% operator is the most convenient.

1. Print out a string with% s as a placeholder.

2. Print out an integer with% d as a placeholder.

3. Print out a floating point number, using% f as a placeholder.

Of course, when we see the above floating point printing, there are too many decimal points, we can limit the precision, for example, keep only 2 decimal places, pass%. In the way of digits, the number of digits after the decimal point is the number of digits retained.

For example, keep only 1 decimal place and 0 decimal place:

4. Specify the width of the placeholder (the default right alignment), and the number after% represents the length of the placeholder.

5. Specify the width of the placeholder (left-aligned), the number after% indicates the length of the placeholder, and the front door adds a negative sign.

6. For integers or floating-point numbers, we can specify the padding character (0 or space, default space) of the excess placeholder. In the case of right alignment, write 0 after%.

Note: if you have more than one formatted output, use parentheses to expand multiple variables or values, because adding 0 after left alignment will change the precision, so the fill is only valid for right alignment.

On how to use print () formatted output in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report