I feel like this is a really simple question,
but I need help figuring out.
So, I have the following:
str = 'hello world'
str.split() # ['hello','world']
I want to index ‘world’ but str[1] returns ‘e’, which is the second character in the list.
How do I index by word instead of character?
Please help me out and thank you in advance.
(Please don’t tell me to do str[5:]… I wanna know how to index words in general)
You need to index the result of
my_str.split():(Renamed the variable to
my_strto avoid shadowing the built-in.)Note that
my_str.split()does not changemy_strin any way. String objects are immutable in Python and can’t be changed. Instead,my_str.split()returns a list of strings that can be indexed.