Python中的字符串格式:我可以对所有类型使用%s吗?

时间:2022-11-19 23:19:32

When doing string formatting in Python, I noticed that %s transforms also numbers to strings.

在Python中进行字符串格式化时,我注意到%s也将数字转换为字符串。

>>> a = 1
>>> b = 1.1
>>> c = 'hello'
>>> print 'Integer: %s; Float: %s; String: %s' % (a, b, c)
Integer: 1; Float: 1.1; String: hello

I don't know for other variable types, but is it safe to use %s like this?

我不知道其他变量类型,但是这样使用%s是否安全?

It is certainly quicker than specifying always the type each time.

它肯定比每次总是指定类型更快。

4 个解决方案

#1


13  

using %s automatically calls str on the variable. Since everything has __str__ defined, you should be able to do this without a problem (i.e. no exception will be raised). However, what you actually will have printed is another story ...

使用%s会自动调用变量上的str。由于所有内容都已定义__str__,因此您应该能够毫无问题地执行此操作(即不会引发异常)。然而,你真正打印的是另一个故事......

Note that in newer python code, there's another option which uses the format method:

请注意,在较新的python代码中,还有另一个使用format方法的选项:

'Integer: {}; Float: {}; String: {}'.format(a,b,c)

This works basically the same way except that it is more powerful once you learn the syntax.

这基本上以相同的方式工作,除了它在学习语法后更强大。

#2


5  

Yes, that is exactly what %s means; convert the argument to a string and interpolate it.

是的,这正是%s的意思;将参数转换为字符串并插入它。

You can also specify other forms of conversion, such as %d and %f to convert values to different types of representations. See String Formatting Operations for more details.

您还可以指定其他形式的转换,例如%d和%f,以将值转换为不同类型的表示形式。有关详细信息,请参阅字符串格式化操作

As an alternative, as of Python 2.6 and up you can also use a different form of string formatting using the .format() method:

作为替代方案,从Python 2.6及更高版本开始,您还可以使用.format()方法使用不同形式的字符串格式:

print 'Integer: {0}; Float: {1}; String: {2}'.format(a, b, c)

That method offers some extra features, such as referring to attributes of objects, keys in a mapping or indexes in a list to refer to values.

该方法提供了一些额外的功能,例如引用对象的属性,映射中的键或列表中的索引来引用值。

#3


1  

Another option is to use new style string formatting - which behaves the same way.

另一个选择是使用新样式字符串格式 - 其行为方式相同。

>>> 'integer: {} float: {} string: {}'.format(1, 1.1, 'blah')
'integer: 1 float: 1.1 string: blah'

However, it also means you can instead of writing str(obj) do instead:

但是,这也意味着您可以代替编写str(obj)来代替:

>>> format(1)
'1'
>>> format(1.1)
'1.1'
>>> format('blah')
'blah'

And then, you could supply a formatting option to it:

然后,您可以为它提供格式化选项:

>>> format(12345, '>10,')
'    12,345'

#4


1  

The reason there are other operators than %s is to provide specific formatting functionality for the datatypes they operate on.

除了%s之外还有其他运算符的原因是为它们运行的​​数据类型提供特定的格式化功能。

When you use %s, all you get is the result of calling str() on the value to interpolate.

当你使用%s时,所得到的就是对要插值的值调用str()的结果。

So, using %s means you would be unable to force the representation of the value to conform to the strict formatting you may want to use, because you are stuck with the output of str().

因此,使用%s意味着您将无法强制值的表示符合您可能要使用的严格格式,因为您坚持使用str()的输出。

Using the other operators, it is possible to specify a format for the value. IE: perhaps you wish to have all floating point numbers in the string represented to 2 decimal places regardless of the actual number of decimal places the number requires - for this case %s just would not do the job.

使用其他运算符,可以指定值的格式。 IE:也许你希望将字符串中的所有浮点数表示为2位小数,而不管数字所需的实际小数位数 - 对于这种情况,%s只是不能完成这项工作。

#1


13  

using %s automatically calls str on the variable. Since everything has __str__ defined, you should be able to do this without a problem (i.e. no exception will be raised). However, what you actually will have printed is another story ...

使用%s会自动调用变量上的str。由于所有内容都已定义__str__,因此您应该能够毫无问题地执行此操作(即不会引发异常)。然而,你真正打印的是另一个故事......

Note that in newer python code, there's another option which uses the format method:

请注意,在较新的python代码中,还有另一个使用format方法的选项:

'Integer: {}; Float: {}; String: {}'.format(a,b,c)

This works basically the same way except that it is more powerful once you learn the syntax.

这基本上以相同的方式工作,除了它在学习语法后更强大。

#2


5  

Yes, that is exactly what %s means; convert the argument to a string and interpolate it.

是的,这正是%s的意思;将参数转换为字符串并插入它。

You can also specify other forms of conversion, such as %d and %f to convert values to different types of representations. See String Formatting Operations for more details.

您还可以指定其他形式的转换,例如%d和%f,以将值转换为不同类型的表示形式。有关详细信息,请参阅字符串格式化操作

As an alternative, as of Python 2.6 and up you can also use a different form of string formatting using the .format() method:

作为替代方案,从Python 2.6及更高版本开始,您还可以使用.format()方法使用不同形式的字符串格式:

print 'Integer: {0}; Float: {1}; String: {2}'.format(a, b, c)

That method offers some extra features, such as referring to attributes of objects, keys in a mapping or indexes in a list to refer to values.

该方法提供了一些额外的功能,例如引用对象的属性,映射中的键或列表中的索引来引用值。

#3


1  

Another option is to use new style string formatting - which behaves the same way.

另一个选择是使用新样式字符串格式 - 其行为方式相同。

>>> 'integer: {} float: {} string: {}'.format(1, 1.1, 'blah')
'integer: 1 float: 1.1 string: blah'

However, it also means you can instead of writing str(obj) do instead:

但是,这也意味着您可以代替编写str(obj)来代替:

>>> format(1)
'1'
>>> format(1.1)
'1.1'
>>> format('blah')
'blah'

And then, you could supply a formatting option to it:

然后,您可以为它提供格式化选项:

>>> format(12345, '>10,')
'    12,345'

#4


1  

The reason there are other operators than %s is to provide specific formatting functionality for the datatypes they operate on.

除了%s之外还有其他运算符的原因是为它们运行的​​数据类型提供特定的格式化功能。

When you use %s, all you get is the result of calling str() on the value to interpolate.

当你使用%s时,所得到的就是对要插值的值调用str()的结果。

So, using %s means you would be unable to force the representation of the value to conform to the strict formatting you may want to use, because you are stuck with the output of str().

因此,使用%s意味着您将无法强制值的表示符合您可能要使用的严格格式,因为您坚持使用str()的输出。

Using the other operators, it is possible to specify a format for the value. IE: perhaps you wish to have all floating point numbers in the string represented to 2 decimal places regardless of the actual number of decimal places the number requires - for this case %s just would not do the job.

使用其他运算符,可以指定值的格式。 IE:也许你希望将字符串中的所有浮点数表示为2位小数,而不管数字所需的实际小数位数 - 对于这种情况,%s只是不能完成这项工作。