我想这一点,其中i
是一个整数:包括在打印语句中的整
sys.stdout.write('\thello world %d.\n' % i+1)
和它说:“不能连接STR和INT”。我曾尝试各种组合:
int(i) + 1
i + int(1)
...但它不工作
我想这一点,其中i
是一个整数:包括在打印语句中的整
sys.stdout.write('\thello world %d.\n' % i+1)
和它说:“不能连接STR和INT”。我曾尝试各种组合:
int(i) + 1
i + int(1)
...但它不工作
sys.stdout.write('\thello world %d.\n' % (i+1))
心灵括号。
(该%
运算符与比+
运营商更紧密,所以你拉闸努力加1格式的字符串,这是一个错误。)
如何:
sys.stdout.write('\thello world %d.\n' % (i+1))
的Python ('...'%i)+ 1
str.format
如果您的Python版本足够新以支持它(Python2.6 +)†看到您甚至不需要担心这里的%
和+
的优先级。
sys.stdout.write('\thello world {}.\n'.format(i+1))
或作为问题标题所暗示 - 使用print语句
print '\thello world {}.'.format(i+1)
在Python3,print
是一个函数,所以你需要调用它
print('\thello world {}.'.format(i+1))
†在Python2.6中,您需要使用{0}
而不是普通的{}