程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python string formatting output syntax summary

編輯:Python

1. introduction

In programming , The processing of strings is an inevitable task . Common operations for Strings ,Python Built in various tools , This article is about Python Various syntax for string formatted output in .

2. Old style grammar

stay Python The earliest way in is to use symbols % To set the format string . For example, in the following way :

person = 'Sean'
print("My name is %s" %person)

The old grammar uses % Symbols to tell Python The compiler needs to replace this with a string . And to the next % Variable found at symbol person, And change the variable person The value of is carried into the first % In symbols .

You can find the first % The symbol is followed by a s Letter . This letter represents the string type . Other types that can be converted are listed below :

3. New grammar –format

Here is the distinction between old and new grammar , In order to Python 3 As the time point of division .Python 3 This new grammar was introduced in . The new grammar has lost the original % Symbol , change to the use of sth. format() Method to insert a variable into a string , Use it with curly brackets for identification .

For example, there is a string My name is xxx. And I want to change the variable person Insert the value of into the string , Replace the inside xxx, We can use the following way to express :

person = 'Sean'
print("My name is {}".format(person))

You can see curly braces { } To replace the xxx, and .format(person) Can be person The value of the variable is inserted in curly braces .

In the old grammar, we need to follow the % The sequence of symbols is used to output the values of different types of variables , But there is no such restriction in the new grammar , We can specify the order . As shown below :

a = 'Sean'
b = 'John'
c = 'Luis'
print("{},{} and {} are my friends.".format(a, b, c))

The above code uses positional parameters . Where curly braces { } The order of represents .format() The order of parameter values in the method . The first is a、 The second is b, And so on . meanwhile , We can use the numbering parameter to set the replacement field , To change the order , as follows :

a = 'Sean'
b = 'John'
c = 'Luis'
print("{2},{1} and {0} are my friends.".format(a, c, b))
# John,Luis and Sean are my friends.

4. The disadvantages of the new grammar

As a whole , Use format() The syntax for formatting strings is more powerful than the old syntax , It's quite convenient to use ,Python3 It is also recommended that you use this method .

However, this format is not without drawbacks . for instance , When format() When you need to handle multiple parameters or longer strings , The whole code will seem too verbose . Here is a simple example :

first_name = 'John'
last_name='Snow'
city = 'Winterfell'
print("Hi, My name is {} {}. I am from {} ".format(first_name, last_name,city ))

therefore ,Python 3.6 Some changes have been made .

5. Python3.6 in the future :f-string

from Python 3.6 after , Start using f-strings As a way of string formatting .f-strings Also known as ·string interpolation·.

for example , The above code can be changed to f-strings Shown by the following :

person = 'Sean'
print(f"My name is {person}")
# My name is Sean

f-String It is more format() Simple and convenient , It begins with f, Next curly bracket { } Put the parameter name inside , The entire output string uses double quotation marks “ ” identification .

This new approach enables us to Python Expressions are embedded directly in strings . You can embed various expressions , For example, calculate the result directly from the string .

x =10
y = 3
print(f" {x} Add {y} be equal to {x+y} ")
# 10 Add 3 be equal to 13

Python The compiler will put f-string Into a series of strings and expressions , And then combined into the final string output .

6. summary

String formatted output can increase the readability of the program , It can also reduce the error probability of the program , Improve the convenience of maintenance .
Last , If you are using Python3.6 Previous versions , It is recommended to use .format() Format the string ; If it is Python3.6 Later versions , It is suggested that f-string grammar .

You've learned to waste ?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved