Python 字符串格式中的 %s 和 %d 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4288973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between %s and %d in Python string formatting?
提问by karlzt
I don't understand what %sand %ddo and how they work.
我不明白它们是什么%s、%d做什么以及它们是如何工作的。
回答by moinudin
They are used for formatting strings. %sacts a placeholder for a string while %dacts as a placeholder for a number. Their associated values are passed in via a tuple using the %operator.
它们用于格式化字符串。%s充当字符串的占位符,同时%d充当数字的占位符。它们的关联值使用%运算符通过元组传入。
name = 'marcog'
number = 42
print '%s %d' % (name, number)
will print marcog 42. Note that name is a string (%s) and number is an integer (%d for decimal).
将打印marcog 42。请注意,name 是一个字符串 (%s),而 number 是一个整数(%d 表示十进制)。
See https://docs.python.org/3/library/stdtypes.html#printf-style-string-formattingfor details.
有关详细信息,请参阅https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting。
In Python 3 the example would be:
在 Python 3 中,示例是:
print('%s %d' % (name, number))
回答by Lucas Jones
They are format specifiers. They are used when you want to include the value of your Python expressions into strings, with a specific format enforced.
它们是格式说明符。当您想将 Python 表达式的值包含到字符串中时使用它们,并强制执行特定格式。
See Dive into Pythonfor a relatively detailed introduction.
比较详细的介绍见Dive into Python。
回答by Soviut
%sis used as a placeholder for string values you want to inject into a formatted string.
%s用作要注入格式化字符串的字符串值的占位符。
%dis used as a placeholder for numeric or decimal values.
%d用作数字或十进制值的占位符。
For example (for python 3)
例如(对于python 3)
print ('%s is %d years old' % ('Joe', 42))
Would output
会输出
Joe is 42 years old
回答by kevin
These are placeholders:
这些是占位符:
For example: 'Hi %s I have %d donuts' %('Alice', 42)
例如: 'Hi %s I have %d donuts' %('Alice', 42)
This line of code will substitute %s with Alice (str) and %d with 42.
这行代码将用 Alice (str) 替换 %s,用 42 替换 %d。
Output: 'Hi Alice I have 42 donuts'
输出: 'Hi Alice I have 42 donuts'
This could be achieved with a "+" most of the time. To gain a deeper understanding to your question, you may want to check {} / .format() as well. Here is one example: Python string formatting: % vs. .format
大多数情况下,这可以通过“+”来实现。为了更深入地了解您的问题,您可能还需要检查 {} / .format()。这是一个示例:Python 字符串格式:% vs. .format
also see here a google python tutorial video @ 40', it has some explanations https://www.youtube.com/watch?v=tKTZoB2Vjuk
也可以在这里看到一个 google python 教程视频@ 40',它有一些解释 https://www.youtube.com/watch?v=tKTZoB2Vjuk
回答by Sujatha
In case you would like to avoid %s or %d then..
如果您想避免 %s 或 %d 那么..
name = 'marcog'
number = 42
print ('my name is',name,'and my age is:', number)
Output:
输出:
my name is marcog and my name is 42
回答by Leo
%dand %sare placeholders, they work as a replaceable variable. For example, if you create 2 variables
%d并且%s是占位符,它们用作可替换的变量。例如,如果您创建 2 个变量
variable_one = "Stackoverflow"
variable_two = 45
you can assign those variables to a sentence in a string using a tuple of the variables.
您可以使用变量元组将这些变量分配给字符串中的句子。
variable_3 = "I was searching for an answer in %s and found more than %d answers to my question"
Note that %sworks for String and %dwork for numerical or decimal variables.
请注意,它%s适用于 String 并%d适用于数字或十进制变量。
if you print variable_3it would look like this
如果你打印variable_3它看起来像这样
print(variable_3 % (variable_one, variable_two))
I was searching for an answer in StackOverflow and found more than 45 answers to my question.
我在 StackOverflow 中寻找答案,发现我的问题有超过 45 个答案。
回答by Stiffy2000
The %dand %sstring formatting "commands" are used to format strings. The %dis for numbers, and %sis for strings.
在%d与%s字符串格式化“命令”用于格式字符串。的%d是数字,%s是用于字符串。
For an example:
例如:
print("%s" % "hi")
and
和
print("%d" % 34.6)
To pass multiple arguments:
传递多个参数:
print("%s %s %s%d" % ("hi", "there", "user", 123456))will return hi there user123456
print("%s %s %s%d" % ("hi", "there", "user", 123456))将返回 hi there user123456
回答by venergiac
from python 3 doc
%dis for decimal integer
%d是十进制整数
%sis for generic string or object and in case of object, it will be converted to string
%s用于通用字符串或对象,如果是对象,它将被转换为字符串
Consider the following code
考虑以下代码
name ='giacomo'
number = 4.3
print('%s %s %d %f %g' % (name, number, number, number, number))
the out put will be
输出将是
giacomo 4.3 4 4.300000 4.3
贾科莫 4.3 4 4.300000 4.3
as you can see %dwill truncate to integer, %swill maintain formatting, %fwill print as float and %gis used for generic number
如您所见,%d将截断为整数,%s将保持格式,%f将打印为浮点数并%g用于通用数字
obviously
明显地
print('%d' % (name))
will generate an exception; you cannot convert string to number
会产生异常;您不能将字符串转换为数字
回答by a_m_dev
speaking of which ...
python3.6 comes with f-stringswhich makes things much easier in formatting!
now if your python version is greater than 3.6 you can format your strings with these available methods:
说到这……
python3.6 自带的,f-strings这让格式化变得更容易了!
现在,如果您的 python 版本大于 3.6,您可以使用以下可用方法格式化字符串:
name = "python"
print ("i code with %s" %name) # with help of older method
print ("i code with {0}".format(name)) # with help of format
print (f"i code with {name}") # with help of f-strings
回答by Agnel Vishal
As per latest standards, this is how it should be done.
按照最新的标准,这应该是这样做的。
print("My name is {!s} and my number is{:d}".format("Agnel Vishal",100))
Do check python3.6 docsand sample program

