python字符串格式化是什么意思_Python字符串格式化中%s和%d之间有什么区别?...

Python字符串格式化中%s和%d之间有什么区别?

我不明白%s和%d做了什么以及它们是如何工作的。

10个解决方案

149 votes

它们用于格式化字符串。 marcog 42用作字符串的占位符,而%d用作数字的占位符。 它们的关联值通过使用%运算符的元组传递。

name = 'marcog'

number = 42

print '%s %d' % (name, number)

将打印marcog 42.请注意,name是一个字符串(%s),number是一个整数(%d表示十进制)。

有关详细信息,请参见[https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting]。

在Python 3中,示例将是:

print('%s %d' % (name, number))

marcog answered 2019-08-01T04:07:09Z

25 votes

%d用作要注入格式化字符串的字符串值的占位符。

%d用作数字或小数值的占位符。

例如(对于python 3)

print ('%s is %d years old' % ('Joe', 42))

会输出

Joe is 42 years old

Soviut answered 2019-08-01T04:07:55Z

14 votes

来自python 3 doc

%d是十进制整数

%d用于通用字符串或对象,如果是对象,则将其转换为字符串

请考虑以下代码

name ='giacomo'

number = 4.3

print('%s %s %d %f %g' % (name, number, number, number, number))

输出将是

giacomo 4.3 4 4.300000 4.3

正如你所看到的%d将截断为整数,%s将保持格式化,%f将打印为float,%g用于通用数字

明显

print('%d' % (name))

会产生异常; 你不能将字符串转换为数字

venergiac answered 2019-08-01T04:09:10Z

11 votes

这些是占位符:

例如:'Hi Alice I have 42 donuts'

这行代码将用%(str)替换%s,用42替换%d。

产量:'Hi Alice I have 42 donuts'

这可以通过大多数时间的“+”来实现。 为了更深入地理解您的问题,您可能还需要检查{} / .format()。 这是一个例子:Python字符串格式:%vs. .format

另见这里的谷歌python教程视频@ 40',它有一些解释[https://www.youtube.com/watch?v=tKTZoB2Vjuk]

kevin answered 2019-08-01T04:10:11Z

9 votes

%d和%s是占位符,它们作为可替换变量。 例如,如果您创建2个变量

variable_one = "Stackoverflow"

variable_two = 45

您可以使用变量元组将这些变量分配给字符串中的句子。

variable_3 = "I was searching for an answer in %s and found more than %d answers to my question"

请注意,variable_3适用于String,%d适用于数字或十进制变量。

如果你打印variable_3它会是这样的

print(variable_3 % (variable_one, variable_two))

我在StackOverflow中搜索答案,发现我的问题超过45个答案。

Leo answered 2019-08-01T04:11:06Z

9 votes

print("%s %s %s%d" % ("hi", "there", "user", 123456))和hi there user123456字符串格式化“命令”用于格式化字符串。 %d用于数字,%s用于字符串。

举个例子:

print("%s" % "hi")

print("%d" % 34.6)

传递多个参数:

print("%s %s %s%d" % ("hi", "there", "user", 123456))将返回hi there user123456

Stiffy2000 answered 2019-08-01T04:11:50Z

7 votes

它们是格式说明符。 当您希望将Python表达式的值包含在字符串中时,会使用它们,并强制执行特定格式。

有关详细介绍,请参阅Dive into Python。

Lucas Jones answered 2019-08-01T04:12:25Z

2 votes

如果您想避免%s或%d,那么..

name = 'marcog'

number = 42

print ('my name is',name,'and my age is:', number)

输出:

my name is marcog and my name is 42

Sujatha answered 2019-08-01T04:12:53Z

1 votes

说到哪......

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

a_m_dev answered 2019-08-01T04:13:36Z

0 votes

按照最新标准,这是应该如何做的。

print("My name is {!s} and my number is{:d}".format("Agnel Vishal",100))

检查python3.6文档和示例程序

Agnel Vishal answered 2019-08-01T04:14:10Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值