In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to output python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
%-formatting
This method uses% plus specific characters to form a placeholder-like function, and then fills the passed parameters into the placeholder space sequentially, usually we use% s to indicate that the occupied space is a string, and use% d to indicate that the occupied space is a number (integer number).
Print ("string type output first:% s"% "hello word") print ("integer type output first:% d"% 0) string type output first: hello word
The first type of integer output: 0
The most common output method in python is this method, which is also closest to the formatted output of other languages (such as C), and it is faster to get started with other language bases.
Usage of str.format ()
Format () is more powerful than the previous formatting output method, which treats the string as a template, formats it with passed parameters, and uses curly braces'{}'as a placeholder character instead of'%'.
Echo = "hello world" num = 0 print ("string type output second: {}" .format (echo)) print ("integer type output second: {}" .format (num)) string type output second: hello world
The second type of integer output: 0
This approach is more complex than the previous one, but it is correspondingly more powerful.
F-string usage
The format string contains the "replacement field" {} enclosed in curly braces. Anything not contained in curly braces is treated as text and is copied as is to the output. A new string formatting method added by Python3.6 in the format f "{variable}, using curly braces {} to indicate the character to be replaced. F-string is not a string constant in nature, but an expression that evaluates at run time:
Echo = "hello world" num = 0print (f "string type output third: {echo}") print (f "integer type output third: {num}") string type output third: hello world
The third type of integer output: 0
As a unique function of the high version of python, F-string uses a special computing method to make it the highest performance of the three formatted output modes. And the operation is also more convenient, the editor highly recommended!
Other output modes
In python, print is not the only one that can output in the console, logging can also be used, and logging can also write the output text to the log while outputting in the console. If you use print to debug the code, it is recommended that you use logging to get a better development experience.
Import logginglogging.basicConfig (format='% (asctime) s -% (pathname) s [line:% (lineno) d] -% (levelname) s:% (message) slots, level=logging.DEBUG) logging.debug ('debug information') logging.info ('info information') logging.warning ('warning information') logging.error ('error information') logging.critical ('critial information') "how python is output" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.