0

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
asked Sep 25, 2014 at 0:13
1
  • 1
    string.split() you need to add parens to call the method, also try to avoid using string as a variable name. Commented Sep 25, 2014 at 0:15

4 Answers 4

2

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()

answered Sep 25, 2014 at 0:15
1
  • ohh.. wait let me try that. I'll let u know what I get. Commented Sep 25, 2014 at 0:15
0

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]
answered Sep 25, 2014 at 0:17
0

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()
answered Sep 25, 2014 at 0:18
0

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

answered Sep 25, 2014 at 0:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.