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

The use of format method in python

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

# Field width, precision, and thousand-bit delimiter # 100000000000 allows a numeric value to be output in a width range of 2, if the value is less than 12 digits Fill in the blanks on the left with 4 bits print ("a: {num:2}" .format (num = 32)) # a:32print ("a: {num:4}" .format (num = 32)) # aVue 32 with two spaces including 32. These two spaces are four widths # field widths can be used to make a table # create tableprint ("{header1:10} {header2:6} {header3:0}" .format (header1= "name") in python. Header2= "age", header3=2) # name age 2 # explain the results output above # name followed by age (including age is 10 widths) age to 2 (age excluding 2) is 6 width 2 there are no spaces behind 0 width print ("{header1:10} {header2:2}" .format (header1= "name") Header2= "age") print ("{cell11:10} {cell12:2}" .format (cell11 = "Bill", cell12=43)) print ("{cell21:10} {cell22:4}" .format (cell21 = "Mike") Cell22=34)) name age Bill 43Mike 34 Note the number is the left filling space for the string is the right filling space from math import piprint ("float number: {pi:.3f}" .format (pi = pi)) # float number:3.142print ("float number: {pi:20.4f}" .format (pi = pi)) # float number: 3.14 explanation Explanation: contains a 20-bit # intercept string print ("{msg:.5}" .format (msg = "Hello World")) # Hello# explains the interception of the first five strings print ("{msg:.3}" .format (msg = "Hello World")) # Hel# thousand delimiter print ("One googol is {: } ".format (10 * * 10) # One googol is 100000000000 symbol, Align and fill with 0 # 12 00000012from math import piprint ("{pi:012.3f}" .format (pi = pi)) # 00000003.142print ("{pi:#12.3f}" .format (pi = pi)) # 3.142 also indicates blank padding # (right-aligned) defaults to right-aligned print ("{pi:#12.3f}" .format (pi = pi)) #? ? 3.142print ("{pi:0=12.3f}" .format (pi =-pi)) #-0000003.142print ("{pi:?=12.3f}" .format (pi =-pi)) # -? 3.142print ("{pi:=12.3f}" .format (pi =-pi)) #-3.142

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