Python - 模数格式变量

标签 python modulus

当我在 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/

相关文章:

javascript - 如何按升序对奇数数组进行排序,但将偶数保留在其位置?

java - 从纪元开始以秒为单位查找日期,模数不起作用

python - 有没有办法将脚本中的答案列表转换为产生的值?

Python wx 导入失败使用 Anaconda 的 Mac OS X 10.9.5

javascript - 如何让模数运算符在 JavaScript 中的两个数字之间添加奇数?

c# - 为什么 int.MinValue % -1 会导致 OverflowException

loops - 在每个 3d 元素 Twig 循环后添加 div,模数

Python 保存空图

python - Django - 在管理界面中向内联表单添加一个额外字段

python - 在 Python 中找出代理类型(http,socks 4/5)?