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
4 Answers 4
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()
-
ohh.. wait let me try that. I'll let u know what I get.Andre– Andre2014年09月25日 00:15:59 +00:00Commented Sep 25, 2014 at 0:15
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]
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()
To answer the question, you can use index(v)
to find a value's index :).
string.split()
you need to add parens to call the method, also try to avoid usingstring
as a variable name.