Andre
Andre

Reputation: 1661

How to find index of string in List

Sorry if this is a noob question. But I am new to programming so I am still learning.

I have a string

string = "Hello Bye Hi"

I split it up:

new_list_string = string.split

output:

["Hello", "Bye", "Hi"]

My question is how can I use this newly generated list, for example.

if I do this:

 new_list_string[1]

I don't get "Bye", instead I get an error:

builtins.TypeError: 'builtin_function_or_method' object is not iterable

Upvotes: 0

Views: 75

Answers (4)

Jason
Jason

Reputation: 3917

To answer the question, you can use index(v) to find a value's index :).

Upvotes: 0

chapelo
chapelo

Reputation: 2562

try this...

new_list_string()[1]

although it is better not to use string as a variable, because it is the name of a python module. Anyway, you should use...

s.split()

Upvotes: 0

Steven
Steven

Reputation: 525

try new_list_string = string.split()
then you should be able to access

new_list_string[0]
new_list_string[1]
new_list_string[2]

Upvotes: 0

corvid
corvid

Reputation: 11197

the problem is you did string.split, and not string.split()

Deeper explanation: when you do string.split, you never actually call split. Therefore, it returns the function rather than the list. You need to call it with the syntax string.split()

Upvotes: 2

Related Questions