我是新手与Python formatting.I已分配给如下的变量k的整数:Python..adding INT到可变
k = 1
tel = tel + [(%d + 1) % k]
print tel
欲与%d +1
上面的代码不起作用,因为我确信语法中有错误。我如何写出正确的表格
谢谢!
我是新手与Python formatting.I已分配给如下的变量k的整数:Python..adding INT到可变
k = 1
tel = tel + [(%d + 1) % k]
print tel
欲与%d +1
上面的代码不起作用,因为我确信语法中有错误。我如何写出正确的表格
谢谢!
好像你想要的是在python.which str()
功能强制将数据转化为字符串
k = 1
tel = tel + str(k+1)
print tel
连接字符串与整数是首先将整数转换为字符串的方式。代码应该看起来像这样!
k = 1;
tel = ‘tel%d’ % str(1 % k);
print tel;
它不起作用。你错误地引用了''',但是当你将'1%k'转换为字符串时,它应该是'%s'。不确定你想做模操作,你可能想写'1 + k' – jrjc 2014-10-06 06:30:04
如果tel
是一个字符串:
k = 1
tel = "tel%i1" % k
# or
# tel = "tel" + str(k) +"1"
print tel
tel11
如果它是一个变量:
tel = "foo"
k = 1
tel = "%s%i1" %(tel, k)
# or
# tel = tel + str(k) + "1"
print tel
foo11
如果你想在连接前执行加法:
k = 1
tel = "tel%i" %(k+1)
# or
# tel = "tel" + str(k+1)
print tel
tel2
希望这帮助。
要将字符串与整数连接起来,您应该将整数转换为字符串(str())。
打印“你好” + STR(1)等等
希望将该W
什么是 “%d” 应该是什么? – 2014-10-06 05:29:07
在这里检查http://stackoverflow.com/questions/2847386/python-string-and-integer-concatenation – Himanshu 2014-10-06 05:29:14
我仍然不知道为什么你使用%d? – 2014-10-06 05:31:16