笨方法学python--字符串和文本

时间:2023-03-08 18:08:24

1print 变量

abc = False

joke_string = "Isn't that joke so funny?! %r"

print joke_string %abc

结果:

Isn't that joke so funny?! False

若改成下面这样:

print joke_string + abc

会报错:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'bool' objects

2 报错:not all arguments converted during string formatting

发生这个错误的原因是写的%的格式化字符串数量大于给出的变量数量。

例如:

>>> a=123
>>> b=234
>>> c=456

>>> print "%s,%s" %(a,b,c)

若是:

>>> print "%s,%s" %a

就会报 TypeError: not enough arguments for format string