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

Python how to use% operator to format string in detail

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Python how to use the% operator format string detailed explanation, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Preface

The% operator of Python can be used to format strings and control the rendering format of strings.

The template format for formatting a string with the% operator is as follows:

% [(name)] [flags] [width]. [precision] typecode

1. Parameters of string template

Name: optional parameter to specify the key of the dictionary when the value that needs to be formatted is of dictionary type.

Flags: optional parameter. Available values are as follows:

+: indicates right alignment, with a positive sign before a positive number and a negative sign before a negative number.

-: indicates left alignment, with no sign before a positive number and a negative sign before a negative number.

Spaces: indicates right alignment, adding spaces before positive numbers and minus signs before negative numbers.

0: indicates right alignment, there is no sign before a positive number, a negative sign is added before a negative number, and 0 is used to fill in the blanks.

Width: optional parameter that specifies the occupancy width of the format string.

Precision: an optional parameter that specifies the number of decimal places reserved for numeric data.

Typecode: a required parameter that specifies the format control character.

2. Format control character

Format controllers are used to control the display of different symbols in a string template, for example, as strings, integers, floating-point numbers, and so on.

Example 3-2 formats the string with the% operator:

# display decimal numbers Convert floating point numbers to decimal numbers > "% d% d"% (121412.3) '1212numbers # set display width of decimal numbers > "% 6d% 6d"% (1212cm) # set display width and alignment of decimal numbers > "%-6d"% (12)' 12numbers # display as floating points > "% f"% (100) '100.000000numbers # display as floating point numbers And set its width and decimal places > "% 6.2f"% (100) '100.00 1.000000e+02'# # to display strings and integers in scientific enumeration table > "% e"% (100)' 1.000000e+02'#, and set their width > "s is%-3D years old"% ("Rose", 18) 'Rose is 18 years old'3, formatting operator auxiliary character, respectively.

Strings can be formatted with "%", but "%" is often used in conjunction with the following auxiliaries.

Auxiliary symbol description * defines width or decimal point precision-used for left alignment + displays the plus sign (+) before positive numbers # displays zeros (0) before octal numbers Displaying "0x" or "0X" (depending on whether you use "x" or "X") before the hexadecimal number is preceded by "0" instead of the default space (var) mapping variable (usually used to deal with the parameter of the field type) m.nm is the minimum total width displayed, n is the number of digits after the decimal point (if available) will it help you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report