Error trying to put Python strings in the list one by one. TypeError: string indices must be integers, not str

Asked 2 years ago, Updated 2 years ago, 42 views

I'm trying to cut strings one by one and put them on the list, but I keep getting errors. I'd appreciate it if you could tell me what the problem is.

string = "Hello World!"

list = []

for i in string:

     list.append(string[i])

print(list)

python string list

2022-09-20 19:34

1 Answers

You're saying there's an error like this, right?

TypeError: string indices must be integers, not str
(TypeError: The index of the string must not be text but must be an integer.)

In fori in string:, i is "H", "e", "l", "l" respectively. 0, 1, 2... Not (string) because it is a string. It's a Python feature.)


2022-09-20 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.