Python Forum

Full Version: Unable to understand the function string.split()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I do the following it works:

"A tree in the park".split()
Output:
['A', 'tree', 'in', 'the', 'park']
But when I do the following, it doesn't work (returns an error):

sentence = ["A tree in the park"]
print(sentence.split())
The error is as follows:
Error:
Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> print(sentence.split()) AttributeError: 'list' object has no attribute 'split'
The following works though:
sentence = "A tree in the park"
print(sentence.split())
The output is as below:
Output:
['A', 'tree', 'in', 'the', 'park']
My hunch is that the split command works only on strings and not lists (that's what I got from the error message, an AttributeError). However, I was taught string manipulation along with lists and I assumed that all commands/built-in functions work for both strings and lists.

Can anyone please clarify the problem for me? Gracias.
(Sep-13-2024, 07:03 AM)Hudjefa Wrote: [ -> ]However, I was taught string manipulation along with lists and I assumed that all commands/built-in functions work for both strings and lists.

Can anyone please clarify the problem for me? Gracias.

Obviously, the problem is that your assumption is not correct
str and list are very different things. Both can be indexed, and both can be iterated, but the similarities end there. A list is a mutable collection of objects. A str is a single, immutable object. It could be argued that a str is more like an int, another immutable object, than a list.
When is a string not a string?

sentence = ["A tree in the park"]
print(sentence[0].split())
['A', 'tree', 'in', 'the', 'park']
Claro?
(Sep-13-2024, 03:28 PM)Pedroski55 Wrote: [ -> ]When is a string not a string?

sentence = ["A tree in the park"]
print(sentence[0].split())
['A', 'tree', 'in', 'the', 'park']
Claro?

Did you convert the string into a list? What does sentence[0] mean here?
(Sep-15-2024, 05:34 AM)Hudjefa Wrote: [ -> ]What does sentence[0] mean here?
Read about lists in the official Python tutorial. Also I strongly recommend to read the whole tutorial.
Quote:...I was taught string manipulation along with lists...

It could be argued that you were taught neither if you don't even understand indexing.
(Sep-15-2024, 06:31 AM)somhairle69tx Wrote: [ -> ]
Quote:...I was taught string manipulation along with lists...

It could be argued that you were taught neither if you don't even understand indexing.

It's a very basic course, titled "Introduction to ... ".
Quote:Did you convert the string into a list? What does sentence[0] mean here?

It is not so arcane or esoteric that only the initiated can understand it.

Python has things called: list

An array in computer languages consists of pairs of values, think of them as name:value.

A list is an integer indexed array. The indexing starts at zero. So, if you want to get the first value in a list, use the name zero.

Here is a list:

mylist = ['a', 'b', 1]
print(mylist[0]) # returns a
print(mylist[1] # returns b
print(mylist[2] # returns 1
So, if you have a list:

sentence = ["A tree in the park", "A tree in the forest", 1]
print(mylist[0]) # returns "A tree in the park"
print(mylist[1] # returns "A tree in the forest",
print(mylist[2] # returns 1
Just try it in your Python IDE, you will soon understand it.

split() will split a string and return a list. You have to give it a string, not a list. Like:

mylist = sentence[0].split()
sentence[0] means the first element in the list called sentence, which is "A tree in the park"
sentence[0] is a string, so you can split it.

No magic, no conversion of any kind.

Just experiment in your IDE, you will soon get the hang of it!

The thing about Python is, you can use names for variables which are fairly understandable. Your list sentence could better be called sentences, then, you can put many sentences in there and look at them 1 by 1 for something.

After you know about Python lists, look at Python dictionaries. A Python dictionary has name:value pairs, but the name can be, but does not have to be, an integer.

mydict = {"first": "A tree in the park", "second": "A tree in the forest", 1: 1}
Python is great because there is so much help on the internet, just search!

¿Vale?