In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to format strings in format in Python", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn how to format strings in format in Python.
I. Preface
Python provides two methods for string formatting
1. Use the percent sign to format strings, and now Python has stopped updating this method
2. Format method of string, which is recommended
What is the format method
String formatting, indexed by {}
Example 1. Pass the position parameter for i in range (0226,25): url = "https://movie.douban.com/top250?start={}&filter=".format(i) print (url) # Features: there is no need to write anything inside the curly braces # crawler, realize the url automatic page turning function 2. Use the location index value to pass the parameter print ('ppt must not have typos. The more painstakingly ppt is made, the uglier it gets.) print ({0} must not have {2}, {0} the more painstaking it is, the more {1} '.format (' ppt', 'ugly', 'typos')) # the numbers can be reused, but the order can be reversed # the results of the above two printouts are the same 3. Use keywords to pass parameters print ('the world is not a beautiful world'). So beauty is worth pursuing') print ('the world is not {key1}, so {key2} is worth going to {key2}' .format (key2=' pursuit', key1=' beautiful') print ('this world is not {0}) So {key2} is worth going to {key2} '.format (' beautiful', key2=' pursue', key1=' beautiful') # mixed use: pass both the position parameter and the keyword parameter, and the keyword parameter must be after the position parameter # the result of the above three printouts is the same
The above methods of parameter transfer can be mixed, but they are not common and will not be introduced too much here.
4. Format specifier print ('{0pur.4f} '.format (3.1415926)) print (3.1416) # the same result for the above two printouts
This is a typical example of keeping 4 decimal places.
General form of replacement character: {field name! Conversion characters: format specifier}
Format specifier standard format:
[[fill] alignment] [plus or minus sign] [#] [0] [width] [grouping option] [. Precision] [type code]
Common type code: B binary d decimal e index count% percentage
5. Thousand separator print ('{:,} '.format (1234567890)) print (1234567890) # the result of the above two printouts is the same 6, other
If you need to output {}, you need to escape using {{}}.
Bulabula''' can also use the format method
The above is all the content of the article "how to format strings in format in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.