格式化字符串遇到 ValueError: incomplete format

我在全球村 2012-07-17 10:14:27
今天在使用格式化字符串时遇到如下报错,可是不明白为什么我重新赋值,再输出后就没有出现问题了,不明白哪里出问题了,请大家指教,应该很简单的,但我就没想明白。


In [1]: a = 'pyth'

In [2]: b = 'on'

In [3]: 's%s%' % (a, b)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/home/lijy/<ipython console> in <module>()

ValueError: incomplete format

In [4]: "s% s%" % (a, b)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/home/lijy/<ipython console> in <module>()

ValueError: incomplete format

In [5]: a = "python"

In [6]: b = "learn"

In [7]: "%s %s" % (a,b)
Out[7]: 'python learn'

In [8]: '%s %s' % (a,b)
Out[8]: 'python learn'

In [9]: a = 'python'

In [10]: b = 'learn'

In [11]: '%s %s' % (a,b)
Out[11]: 'python learn'

In [12]: '%s %s' % (a, b)
Out[12]: 'python learn'

In [13]: '%s%s' % (a, b)
Out[13]: 'pythonlearn'
...全文
4563 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
我在全球村 2012-07-26
  • 打赏
  • 举报
回复
找到问题就好,以后可能发现类似就知道如何解决了。
NN179 2012-07-25
  • 打赏
  • 举报
回复
我竟然也犯了同样的错误。。。怎么会这样。。。
我在全球村 2012-07-17
  • 打赏
  • 举报
回复
哎呀,我太粗心了,太相信自己了,自己都没注意到,谢谢!
bugs2k 2012-07-17
  • 打赏
  • 举报
回复
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> a = 'pyth'
>>> b = 'on'
>>> 's%s%' % (a, b)

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
's%s%' % (a, b)
ValueError: incomplete format
>>> '%s%s' % (a, b)
'python'
>>>

因为格式化字符串中 s 需要位于 % 后面

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧