当我在 Notepad++ 中输入以下 Python 代码时:
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/