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 realize string formatting in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to implement string formatting in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What is string formatting and why is it necessary?

We sometimes browse Douyin / bilibili to see that the cover looks good, but when we enter the live broadcast, we find it, but that's all!

Presumably, the anchor converts the output into what the reader wants to see in some way.

Do you probably understand what formatting is and the necessity of formatting?

Just by analogy, the formatting of our program is not so excessive! The output of 1 is still 1, but it is easier for human beings to understand.

Instead of being slightly formatted like a web anchor (the project is quite large), it has changed from 1 to 0.

After all, programs can't deceive developers.

Necessity: the output is better formatted and easy for developers and users to read.

What are the formatting in the program?

The Internet says that there are three or four, and other libraries may be added in the future.

The academic Committee has developed a variety of languages and roughly summarized the following two major categories:

String placeholder replacement

String keyword substitution

Let's look at the code.

Placeholder formatting the first% placeholder format

The most common placeholder in python is'%'.

Str = "hello"this is a string:% s"% str "this is a string:% s,% s"% (str, str)

When we take a look at the above, we can see that'% s'is replaced by 'hello'' in the output.

The other is that the replacement of multiple'% s'is replaced by a tuple according to the corresponding subscript.

Formatting can basically be done using'% s'.

Okay, with this in mind, let's move on to the following code:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:13 on 2021-10-30 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray Learning Committee # @ XueWeiTag: CodingDemo# @ File: string_format.py# @ Project: helloimport sysfirst = "continuous learning" second = "continuous development" slogan = first + secondprint (slogan) banner = "*" * 16print (banner) slice = slogan [2:4 ] print (slice) print ("learn" in slogan:% s "% (" learn "in slogan) print (" do not learn "not in slogan:% s"% ("do not learn" not in slogan) print (r "print line breaks are output as ordinary characters!") # We can see% above, and the code has mentioned this many times. But print ("% s"% slogan) # is the most commonly used% s format string # print ("% c"% 'ccc') # TypeError:% c requires int or charprint ("% c"%' c') #% c is usually used to force the length of the string to be detected to be 1print ("% c"% 'mine') #% c is usually used to force the length of the string to be detected The output string must be 1number = 102.40101print ("% I signed integer% I"% number) print ("% I signed integer% I"%-number) print ("% d signed integer% d"% number) print ("% d signed integer% d"% number) print ("% u unsigned integer:% u"% number) print ("% u unsigned integer:% u"% integer) Number:% u "%-number) # print (" octal% o "% number) print ("% o "octal% o") print ("% x hexadecimal% x") # print ("hexadecimal% X") print ("% e natural constant e:% e"% number) # print ("% E"% number) print ("% f floating point% f") % number) print ("% g flexible effective display:% g"% number) # guarantees the display of 6 micro-significant digits Choose the decimal method flexibly, or the scientific counting method # print ("% G"% number) print ("% g flexible and effective display:% g"% (number*10001)) # ensures the display of 6 micro-significant digits. Or scientific counting # the following two writing methods need to pay attention to the execution order # print ("% g"% number*10001) # pay attention to this writing # print ("% g"% number**10) # pay attention to this writing

Readers can copy the running code directly, and the academic committee added the running effect diagram:

What is special here is the display of'% u 'unsigned integers, and there is no difference between positive and negative numbers.

That's for sure. Unsigned integers and signed integers are two ways to represent numbers.

But the unsigned has one more bit to put the number than the signed, so the range is larger.

Let's take a look at the digression of the above paragraph or skip it. Let's move on to other formatting.

The second placeholder format

The slight difference from the above is that we will see an obvious subscript.

{0}, {1}, … {n}

If you want to leave several substitutions, please enclose a few {}, but you must give n parameters. For more information, please see https://docs.python.org/3/library/string.html.

This is very simple and straightforward, let's look at the code:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:13 on 2021-10-30 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray Learning Committee # @ XueWeiTag: CodingDemo# @ File: string_format1.py# @ Project: helloimport sysslogan = "continuous Learning and continuous Development" author = "Ray Learning Committee" text = "{0}, {1}" .format (slogan Author) print ("text= 's" text) keyword format

Very easy to understand is similar to the {} pair of large brackets in the middle of the content to be replaced.

This is very simple and straightforward, let's look at the code:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:13 on 2021-10-30 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: XueWeiTag: CodingDemo# @ File: string_format2.py# @ Project: helloimport sysslogan = "continuous learning and development" author = "text= f" {slogan}, {author} "print (" text=% s "% text)

The running effect is as follows:

From here, keyword formatting is more friendly!

Thank you for reading! This is the end of the article on "how to format strings in Python". I hope the above content can be of some help to you, so that 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: 276

*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