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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -