FAQ: String Methods - Splitting Strings II


This community-built FAQ covers the “Splitting Strings II” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Splitting Strings II

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


The following throws the following error when run in python 3 interpreter:

Exception has occurred: AttributeError

‘tuple’ object has no attribute ‘split’

File "C:\dev\Py-1\Test\string_exercises.py", line 47, in <module> author_names = authors.split(",")
authors = "Audre Lorde,Gabriela Mistral,Jean Toomer,An Qi,Walt Whitman,Shel Silverstein,Carmen Boullosa,Kamala Suraiyya,Langston Hughes,Adrienne Rich,Nikki Giovanni"

author_names = authors.split(',')


author_last_names = []
for name in author_names:


code from the error message:

author_names = authors.split(",")

doesn’t match the code you posted:

author_names = authors.split(',')

the error message says authors is a tuple, if i attempt to run the code:


it goes fine. So not sure what goes wrong


Just a small tip in terms of cleanliness, this can be achieved using a simple list comprehension:

author_last_names = [name.split()[-1] for name in author_names]


For the negative index, what is that mean? author_last_names.append (i.split()[-1])

In the script, I try below, but i still don’t know why and how it works. Please inform. Thank you
if [0] return first name. [-1] or [1] return last name.


we can use negative indexes to access the list from the right hand side, so -1 is the right most element in a list or string:

print ['a', 'b', 'c', 'd'][-1] #output: d

so -2 would give c


I see. Thank you so much


can someone explain what does the [-1] contributes to the code?

author_last_names =
for name in author_names:



-1 get the last element from a list. You use negative values to access the list from the right hand side.

authors = "Audre Lorde, William Carlos Williams, Gabriela Mistral, Jean Toomer, An Qi, Walt Whitman, Shel Silverstein, Carmen Boullosa, Kamala Suraiyya, Langston Hughes, Adrienne Rich, Nikki Giovanni"

author_names = authors.split(",")
author_last_names = []
for i in author_names:
  temp = i.split( )[::-1]


Why doesn’t the -1 reference only the very last letter in the author’s names? Earlier in the lesson, we used -1 to select the very last letter in a string, but here it means the last name entirely. I must be missing something.


If you have a list of names and you get the last name, then you’ll get a name
Your confusion seems to be more about what’s in your list/whateveritis than what -1 as an index means, ie you would see the same difference for index 0 which is a simpler case


Thanks for the quick response! After playing around a little bit, I think I figured it out.

authors = "Audre Lorde, William Carlos Williams, Gabriela Mistral, Jean Toomer, An Qi, Walt Whitman, Shel Silverstein, Carmen Boullosa, Kamala Suraiyya, Langston Hughes, Adrienne Rich, Nikki Giovanni"

author_names = authors.split(", ")


author_last_names = []
for name in author_names:

Those final three print lines helped me distinguish between the different kinds of selection. authors[-1] selects the last letter only, because authors is a single string.


I wouldn’t call them different kinds of selection. You’re asking differently typed values for their value at index -1, and they return some value they think is cool.
You’re asking the same way and they behave the same way. You’re just asking from different things.


Yes, I’m asking for the very last element in those lists in the same way each time,

but an element in the authors list is a single letter, whereas an element in the author_last_names list is the entire last name, correct?

Thanks again!


authors isn’t a list at all, and it’s not really a container either (it’s a bunch of text), but it can produce substrings so it does behave a bit like a container

The elements of a list is whatever you put in it, and the “elements” of a string is … more strings (characters, but there’s no character type).


I said list because early in this lesson, this was written:

A string can be thought of as a list of characters.

Like any other list, each character in a string has an index.]


What they mean or should mean is that they both implement behaviours like iteration, access by index, concatenation, and whatever else.
But you can pretty quickly dismiss the idea that they are or can be thought of as lists if you try to do something that only the other one is capable of. Try to upcase a list. Or try to append to a string.

Many types implement those behaviours, but they won’t implement the same set of behaviours and they won’t necessarily mean the same things due to being different things.


Thank you very much for your clarification. That makes sense. That original tip (“They’re all lists!”) must have confused me slightly.


You can implement these behaviours yourself, for example, iteration and subscription (access by index)

class Derp:
    def __iter__(self):
        for _ in range(5):
            yield 'Meow'

    def __getitem__(self, key):
        return 'received key: {}'.format(key)

myvalue = Derp()  # create a Derp
print(myvalue[3])  # a string saying 3 was the key

for value in myvalue:
    print(value)  # 'Meow' 5 times in total

For python2 the first line should instead be:

class Derp(object):

The main thing to note is that it’s Derp that defines what happens, these things aren’t done to the values, rather it’s asked of them “please do this and that”

It’s also the the main idea in object oriented programming (values which are in charge of the data they contain and the behaviour they have), you can go right ahead and ignore all the other things that nobody really seems to be able to explain why it’s useful but will still say it is.