gpt4 book ai didi

Python - 模数格式变量

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:27 25 4
gpt4 key购买 nike

当我将以下内容作为 Python 代码输入到 Notepad++ 中时:

days = "Mon"

print "Here are the days: %s ". % days

我在 Windows Powershell 中得到了这个输出:

File "ex9exp.py", line 4
print "Here are the days: %s ". % days

我不知道为什么会这样。

我打算输出

print "Here are the days: %s ". % days 

成为

Here are the days: Mon  

希望得到一些帮助。

最佳答案

.在字符串之后是一个运算符,在某些语言(如 PHP)中用于附加字符串,但它在 Python 中不起作用,因此代码如下:

days = "Mon"

print "Here are the days: %s ". % days

产生以下输出:

   File "h.py", line 3
print "Here are the days: %s ". % days
^ SyntaxError: invalid syntax

让您知道编译器/解释器并不期待“.”。

问题可以通过删除 . 来解决,如下所示:

days = "Mon"

print "Here are the days: %s " % days

它将以这种方式工作。

关于Python - 模数格式变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com