Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the Python string formatting command

Shulou Source: shulou.com Published: 2022-06-01 06:33:36 09月28日 Update

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!

Tags: Characters strings formats commands data learning output content students grades parentheses appropriate line location name that is ideas situations data structures quantity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia macOS Shulou Tech Info MySQL