Python 字符串格式中的 %s 和 %d 有什么区别? [英] What's the difference between %s and %d in Python string formatting?
本文介绍了Python 字符串格式中的 %s 和 %d 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白 %s
和 %d
的作用以及它们是如何工作的.
I don't understand what %s
and %d
do and how they work.
推荐答案
它们用于格式化字符串.%s
充当字符串的占位符,而 %d
充当数字的占位符.它们的关联值通过使用 %
运算符的元组传入.
They are used for formatting strings. %s
acts a placeholder for a string while %d
acts as a placeholder for a number. Their associated values are passed in via a tuple using the %
operator.
name = 'marcog'
number = 42
print '%s %d' % (name, number)
将打印marcog 42
.请注意,name 是一个字符串(%s),number 是一个整数(%d 表示十进制).
will print marcog 42
. Note that name is a string (%s) and number is an integer (%d for decimal).
参见 https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting 了解详情.
在 Python 3 中,示例为:
In Python 3 the example would be:
print('%s %d' % (name, number))
这篇关于Python 字符串格式中的 %s 和 %d 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文