FAQ: String Methods - Splitting Strings II

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

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

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)

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


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

What have I done wrong?

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


If the string contains only the delimiter as its content, like seen below, why does ‘split’ return two elements list?

test = “*”


Result: [’’, ‘’]

1 Like

Because its counterpart, str.join() needs that pattern in order to reconstruct the string.

>>> test = "*"
>>> test_list = test.split('*')
>>> test_list
['', '']
>>> test_str = '*'.join(test_list)
>>> test_str

Now let’s tweak the list and remove the quotes…

>>> '*'.join([''])
>>> '*'.join([])

Thank you. The ‘join’ examples that are shared makes me wonder about the internal representation of empty list vs that of list with one element that is an empty string! Why are both producing same result?

separator.join(sequence) is concatenating the sequence of strings with separator between them (where separator is a string). A few examples might help-

list_letters = ['a', 'b', 'c']  
''.join(list_letters)  # Out: 'abc'
' '.join(list_letters) # Out: 'a b c'
'*'.join(list_letters)  # Out: 'a*b*c'
# effectively... 'a' + '*' + 'b' + '*' + 'c'

Note that with three values we only have two separators. So if our sequence to iterate was only one item long?

'test'.join(['b'])  # Out: 'b'
'test'.join([''])  # Out: ''

We just get the sequence without any seprators. This includes the single empty string. With a completely empty sequence, e.g. an empty list [] we just get an empty string returned every time''.

''.join([])  # Out: ''
'test'.join([])  # Out: ''