Python% or format should be used to format string?
% Or format
Imperial PK
Python string formatting There are two camps:% and format, what should we choose?
Since the introduction of the format Python2.6 this method after the format string, I think% or format this is simply not a problem. Do not believe you look down.
# Define a coordinate value C = (250, 250) #% formatted using s1 = "enemy coordinates:% s"% c
Obviously the above code will throw a following TypeError:
TypeError: not all arguments converted during string formatting
As the demand for such formatted we need to write the following format ugly job:
# Define a coordinate value C = (250, 250) #% using ugly format ... S1 = "enemy coordinates:% s"% (c, )
And uses the format would not exist the above questions:
# Define a coordinate value C = (250, 250) # format using format s2 = "enemy coordinates: {}". Format (c )
Obviously, above this one we had reason enough for you to use the format in future projects.
New features
Python3.6 added in the F-strings :
In[1]: name = "Q1mi" In[2]: age = 18 In[3]: f"My name is {name}.I'm {age}" Out[3]: "My name is Q1mi.I'm 18"
Common format usage
By location
In[1]: data = ["Q1mi", 18] In[2]: "Name:{0}, Age:{1}".format(*data) Out[2]: 'Name:Q1mi, Age:18'
By Keyword
In[1]: data = {"name": "Q1mi", "age": 18} In[2]: "Name:{name}, Age:{age}".format(**data) Out[2]: 'Name:Q1mi, Age:18'
Object properties
In[1]: class Person(object): ...: def __init__(self, name, age): ...: self.name = name ...: self.age = age ...: def __str__(self): ...: return "This guy is {self.name}, {self.age} years old.".format(self=self) ...: In[2]: p = Person("Q1mi", 18) In[3]: str(p) Out[3]: 'This guy is Q1mi, 18 years old.'
By subscript
In[1]: "{0[0]} is {0[1]} years old.".format(data) Out[1]: 'Q1mi is 18 years old.'
Filling aligned
In[1]: "{:>10}".format('18') Out[1]: ' 18' In[2]: "{:0>10}".format('18') Out[2]: '0000000018' In[3]: "{:A>10}".format('18') Out[3]: 'AAAAAAAA18
A supplementary string comes zfill () Method:
In[1]: "18".zfill(10) Out[1]: '0000000018'
Accuracy and type f
In[1]: "{:.2f}".format(3.1415926) Out[1]: '3.14'
Wherein an accuracy of .2 represents the length of 2, f denotes a float.
Other band
In[1]: "{:b}".format(18) Out[1]: '10010' In[2]: "{:d}".format(18) Out[2]: '18' In[3]: "{:o}".format(18) Out[3]: '22' In[4]: "{:x}".format(18) Out[4]: '12'
Thousands Separator
In[1]: "{:,}".format(1234567890) Out[1]: '1,234,567,890
% Or format
Imperial PK
Python string formatting There are two camps:% and format, what should we choose?
Since the introduction of the format Python2.6 this method after the format string, I think% or format this is simply not a problem. Do not believe you look down.
# Define a coordinate value C = (250, 250) #% formatted using s1 = "enemy coordinates:% s"% c
Obviously the above code will throw a following TypeError:
TypeError: not all arguments converted during string formatting
As the demand for such formatted we need to write the following format ugly job:
# Define a coordinate value C = (250, 250) #% using ugly format ... S1 = "enemy coordinates:% s"% (c, )
And uses the format would not exist the above questions:
# Define a coordinate value C = (250, 250) # format using format s2 = "enemy coordinates: {}". Format (c )
Obviously, above this one we had reason enough for you to use the format in future projects.
New features
Python3.6 added in the F-strings :
In[1]: name = "Q1mi" In[2]: age = 18 In[3]: f"My name is {name}.I'm {age}" Out[3]: "My name is Q1mi.I'm 18"
Common format usage
By location
In[1]: data = ["Q1mi", 18] In[2]: "Name:{0}, Age:{1}".format(*data) Out[2]: 'Name:Q1mi, Age:18'
By Keyword
In[1]: data = {"name": "Q1mi", "age": 18} In[2]: "Name:{name}, Age:{age}".format(**data) Out[2]: 'Name:Q1mi, Age:18'
Object properties
In[1]: class Person(object): ...: def __init__(self, name, age): ...: self.name = name ...: self.age = age ...: def __str__(self): ...: return "This guy is {self.name}, {self.age} years old.".format(self=self) ...: In[2]: p = Person("Q1mi", 18) In[3]: str(p) Out[3]: 'This guy is Q1mi, 18 years old.'
By subscript
In[1]: "{0[0]} is {0[1]} years old.".format(data) Out[1]: 'Q1mi is 18 years old.'
Filling aligned
In[1]: "{:>10}".format('18') Out[1]: ' 18' In[2]: "{:0>10}".format('18') Out[2]: '0000000018' In[3]: "{:A>10}".format('18') Out[3]: 'AAAAAAAA18
A supplementary string comes zfill () Method:
In[1]: "18".zfill(10) Out[1]: '0000000018'
Accuracy and type f
In[1]: "{:.2f}".format(3.1415926) Out[1]: '3.14'
Wherein an accuracy of .2 represents the length of 2, f denotes a float.
Other band
In[1]: "{:b}".format(18) Out[1]: '10010' In[2]: "{:d}".format(18) Out[2]: '18' In[3]: "{:o}".format(18) Out[3]: '22' In[4]: "{:x}".format(18) Out[4]: '12'
Thousands Separator
In[1]: "{:,}".format(1234567890) Out[1]: '1,234,567,890