0

I'm new to Python and I'm attempting to place each individual character of a string into an individual element of an array.

string= 'Hello'
array= []

length_of_string= len(string)-1

for i in range (length_of_string):
    array.append(string(i))

print(array)

However when I run this code an error occurs as shown below.

   array.append(string(i))
TypeError: 'str' object is not callable

The append function works fine when I append to an array using strings or numbers normally but in this instance it does not work.

What do I have to do to get

['H','e','l','l','o']
Georgy
  • 12,464
  • 7
  • 65
  • 73
Babasan883
  • 3
  • 1
  • 2
  • 1
    Anything wrong with `list('Hello')` ? – jpp Mar 22 '18 at 10:29
  • `string(i)` is not correct – it is 'not callable' because it is not a function. (If you use the correct form `string[i]`, you still get a suprising result because of that (surprising!) earlier `-1`.) – Jongware Mar 22 '18 at 10:30
  • Use `string[i]` - as @usr2564301 points out. – jpp Mar 22 '18 at 10:31

1 Answers1

1

You mean string[i] if you wan't the ith element of string (not string(i) -- python isn't matlab). However, it's much faster just to do

list(string)  # ['H','e','l','l','o']
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47