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

Case Analysis of Python formatted string

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "Python formatted string instance analysis". In daily operation, I believe many people have doubts about Python formatted string instance analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Python formatted string instance analysis". Next, please follow the editor to study!

1. Three commonly used formatting string methods 1.% as placeholder name = 'Zhang San' age = 10print ('name% s, this year% d'% (name, age)) # run result: name Zhang San, this year 10

The% placeholder, s and d indicate the filled data type, and the order should be the same as the order of the data variables in parentheses after%

two。 Use format () name = 'Zhang San' age = 10print ('name {0}, this year {1} year old' .format (name, age)) # run result: name Zhang San, 10 years old

{} is a placeholder, 0 means the first data variable in the format parameter, and so on.

3. Format name = 'Zhang San' age = 10print (f 'name {name}, this year {age} years old') # result: name Zhang San, 10 years old

The {data variable name} in the f string must be added before the string to take effect.

two。 String width and precision 1.% filler representation # width 10 run result: 80print ('d'% 80) # keep three decimal places: 3.142print ('.3f'% 3.14159) # keep three decimal places, width 10 run result: 3.142print ('.3f'% 3.1415926)

10 is width. 3f is to retain three decimal places d as the pre-conversion element data type.

Note: if there is more than one data element after%, only the first data element is formatted

The 2.format () notation # .3 represents a total of three numeric run results: 3.14print ('{3.142print .3f} '.format (3.14159)) # .3f represents the result of a three-decimal run: 3.142print (' {03.142print .3f} '.format (3.14159)) # the width of 10 retains the order of three decimal places: 3.142print (' {3.142print 10.3f} '.format (3.14159)) # 0 is the order of placeholders The default value of 0 can be omitted

For example:

# run result: 256.354print ('{1 10.3f} '.format (3.14159, 256.354)) # 1 indicates the placeholder, that is, the order of the parameters in format (), and 1 is the second data element starting from 0-> 256.35x10 indicates the width of the formatted data element # .3f indicates that the precision retains three decimal places. String alignment

1.center () is centered, the first parameter specifies the width, the second parameter specifies the filler, the second parameter is optional, the default is a space, and the original string is returned if the set width is less than the actual width

S = 'hello,python'print (s.center (20,' *)) # result: * hello,python****

2.ljust () is aligned to the left. The first parameter specifies the width, the second parameter specifies the filler, and the second parameter is optional. The default is space. If the set width is less than the actual width, the original string is returned.

S = 'hello,python'print (s.ljust (20)) # run result: hello,python print (s.ljust (20,' *)) # run result: hello,python*print (s.ljust (10)) # run result: hello,python

3.rjust () is aligned to the right. The first parameter specifies the width, the second parameter specifies the filler, and the second parameter is optional. The default is space. If the set width is less than the actual width, the original string is returned.

S = 'hello,python'print (s.rjust (20)) # run result: hello,pythonprint (s.rjust (20,' *)) # run result: * hello,pythonprint (s.rjust (10)) # run result: hello,python

4.zfill () is aligned to the right and filled with 0 on the left. This method takes only one parameter to specify the width of the string. If the specified width is less than or equal to the length of the string, the string itself is returned.

S = 'hello,python'print (s.zfill (20)) # run result: 000000Hellodomain pythonprint (s.zfill (10)) # run result: at this point in hello,python, the study on "Python formatted string instance analysis" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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