Skip to main content
 首页 » 编程设计

Python之模数格式变量

2024年10月25日9xing901022

当我将以下内容作为 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  

它将以这种方式工作。