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!

1 Like

6 posts were split to a new topic: Tuple error?

32 posts were merged into an existing topic: How to select only last names?

A post was merged into an existing topic: How to select only last names?

A post was split to a new topic: This was my solution

A post was merged into an existing topic: How to select only last names?

2 posts were split to a new topic: This is my solution

A post was merged into an existing topic: How to select only last names?

A post was merged into an existing topic: How to select only last names?

2 posts were split to a new topic: How can I improve my code?

2 posts were split to a new topic: My solution

A post was merged into an existing topic: How to select only last names?

3 posts were split to a new topic: How should I use .split() to remove commas between names?

In the 1st exercise, I think that you should allow bellow to work, because you actually want to remove the space between authors

author_names = authors.split(", ")
3 Likes

I agree!
That’s exactly what I did, and was surprised when it was not accepted as right.
I tested with print and it seemed perfect.
I struggled to figure out what the expected goal was.

1 Like

Uhm. Have you looked at the string though? What delimits the authors? It’s not ", "

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

I don’t know how and why, but I checked again and my saved solution looks like this:

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(’,’)
print(author_names)

def grab_last_name(name_list):
** last_name_list = **
** for name in author_names:**
** last_name_list.append(name.split()[-1])**
** return last_name_list**

author_last_names = grab_last_name(author_names)
print(author_last_names)

So, ", " delimits the authors.

Interesting thing is that now when I reset this exercise I get a list of authors delimited only by “,” as you posted.

But then, I tried to paste the first part of my code (including authors list whit blank space) after reset and I still get:
“Value for author_names did not match the expected value.”
List looks perfect:
[‘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’]
Then I remove blank space after “,” and my code is accepted as correct eventhough list looks wrong (whit blank space before names in the list:
[‘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’]

Anyway, I’m quite sure that the list for some reason looked different, and [brunpierreantoine] had the same problem.

I’ve found another discussion on the same problem:

1 Like

Hi,

The code below printed a list of surnames as was required, but the code editor did not accept the solution. This is what was printed out:
[‘Lorde’, ‘Williams’, ‘Mistral’, ‘Toomer’, ‘Qi’, ‘Whitman’, ‘Silverstein’, ‘Boullosa’, ‘Suraiyya’, ‘Hughes’, ‘Rich’, ‘Giovanni’]

#And this was the code:

def author_last_names():
  surnames = []
  for index in range(0, len(author_names)):
    split = author_names[index].split()
    surnames.append(split[-1])
  print(surnames)
 
author_last_names()

What have I done wrong?

Double check the requirements. Does it ask you to print values out or does it mention something else?