In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how python implements string formatting. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Formatting of a string
Name = "Chan" print ("Hi, Isimm" + name)
Usually, if we need to combine or concatenate strings in the program, we use the plus sign to concatenate the strings. If we do the concatenation of two strings, there is no problem with this, but if there are more strings, it is similar to this:
Name = "Chan" country = "China" age = 23 print ("Hi, Ichimm" + name + ".Ihumm from" + country + ".And Isimm" + str (age) + "."
Such a program will be very messy and difficult to read; moreover, when we connect plastic data, we also need to convert the type, otherwise the program will report an error.
In fact, we can write the program like this and use the% sign syntax of Python to format the string, where% s represents a string that will be replaced here,% d means that it will be replaced with a decimal system, and finally, the content in the brackets represents the content that will be replaced:
Name = "Chan" country = "China" age = 23 print ("Hi, Isimm% s. Isimm from% s. And Isimm% d."% (name,country,age))
Although the program looks much better like this, we can do better here. We can use the format function and curly braces syntax in python to write the program like this:
The contents of the curly braces will be replaced with the parameters passed in the format function
Name = "Chan" country = "China" age = 23 print ("Hi, Ihumm {}. ITunm from {}. And Isimm {}." .format (name,country,age))
The contents of the curly braces will be replaced with the parameters passed in the format function. The benefits of using this function are:
You can write the replaced index in curly braces, and the place of the same index will be replaced with the same content, like this:
Name = "Chan" print ("Hi, Isimm {0}. And Isimm {0}." .format (name)) result: Hi, Ichimm Chan. And I'm Chan
The last one, and my favorite, is called f-string. We just need to write an f at the beginning of the string, and the contents of the curly braces are automatically replaced with the value of the specified expression. Note that it is the expression:
Name = "Chan" country = "China" age = 23 print (f "Hi, iTunm {name}. ITunm from {country}. And isimm {age+1}." This is the end of this 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, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.