Converting integer to string in Python?
I want to convert an integer to a string in Python. I am typecasting it in vain:
t=raw_input()
c=[]
for j in range(0,int(t)):
n=raw_input()
a=[]
a,b= (int(i) for i in n.split(' '))
d=pow(a,b)
d.str()
c.append(d[0])
for j in c:
print j
When I try to convert it to string, it's showing an error like int
doesn't have any attribute called str
.
>>> str(10)
'10'
>>> int('10')
10
Links to the documentation:
int()
str()
The problem seems to come from this line: d.str()
.
Conversion to a string is done with the builtin str()
function, which basically calls the __str__()
method of its parameter.
Also, it shouldn't be necessary to call pow()
. Try using the **
operator.
尝试这个:
str(i)
There is not typecast and no type coercion in Python. You have to convert your variable in an explicit way.
To convert an object in string you use the str()
function. It works with any object that has a method called __str__()
defined. In fact
str(a)
is equivalent to
a.__str__()
The same if you want to convert something to int, float, etc.
链接地址: http://www.djcxy.com/p/25188.html上一篇: 在Python中将日期转换为日期时间
下一篇: 在Python中将整数转换为字符串?