python - Appending select character of a string to list -
with string abcdefghijklm
trying achieve following:
abcdefghijklm 0123456789012
the first if
statement works, else
statement breaks with:
position.append(str(x[1]))
typeerror:'int'
object not subscriptable
this code:
number = [] count = 0 x in range(string): if count <= 9: number.append(str(x)) else: number.append(str(x[1])) count = count+1 number = ''.join(map(str, number)) print(number)
how can resolve this?
you can use hack: int(str(s)[1])
or (str(s)[1])
depending on wishes:
>>> s = 12 >>> int(str(s)[1]) 2 >>> (str(s)[1]) '2'
Comments
Post a Comment