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

What is the Python string formatting command

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the Python string formatting command". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the Python string formatting command is.

As we said earlier, you can use% to construct a string, such as

Print'% s is easy to learn'% 'Python'

Sometimes, simply substituting a value does not meet our need to construct a string. Suppose you now have a set of data on student scores, and you want to output them. In one line, you should output both the student's name and his grades. For example

Mike's score is 87.

Lily's score is 95.

In python, you can do this:

Print "% s's score is% d"% ('Mike', 87)

Or

Name = 'Lily'

Score = 95

Print "s's score is d" (name, score)

No matter how many values you need to replace into the string for formatting, just use the% of the corresponding format in the appropriate position in the string, and then provide the substituted values in order in the following parentheses. The placeholder% and the values in parentheses must be equal in quantity and the type must match.

('Mike', 87) this set of data represented by () is called tuple in python and is a basic data structure of python, which we will use later.

Thank you for reading, the above is the content of "what is the Python string formatting command". After the study of this article, I believe you have a deeper understanding of what the Python string formatting command is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report