FAQ: String Methods - Splitting Strings

This community-built FAQ covers the “Splitting Strings” 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

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!

7 posts were split to a new topic: What does the delimiter do?

I don’t fully get this method. In exercise description when you have a code like this:

man_its_a_hot_one = “Like seven inches from the midday sun”
[‘Like’, ‘seven’, ‘inches’, ‘from’, ‘the’, ‘midday’, ‘sun’]

So here it takes every element separated by a space, creates a new list and puts every of those elements as a new element of the list, each on of tchem inside “” and separated by commas.

But in another topic to this exercise I saw in one post code like this:

a = “#”.join(‘mississippi’)
b = a.split(’#’)

Here it took every element that was separated by argument and instead of doing the same thing as above (so [‘m’, ‘i’, ‘s’, ‘s’, ‘i’, ‘s’, ‘i’, ‘p’, ‘i’]) it just “glued” tchem together.

Why is that?

You mean this example?

>>> a = "*".join('mississippi')
>>> a
>>> b = a.split('*')
>>> b

The two methods, str.join() and str.split() are inverse functions. a above is the character sequence with a separator string inserted between characters. When we split on the same character it returns the original sequence.

The same works in reverse. If we split a sequence on a given separator (default is a space character) then we will be able to join with the same character and restore the original state.

So if I use this method when join() was previously used it will just glue tchem together and if I use split on the same code but in a situation when join() was not used it will create a list with separate characters separated with “” and commas. Is my assesment correct?

A sequence of characters, a string, may be split into a sequence of strings, a list. If we do not alter the list, it may be joined again to restore the original string.

>>> a = 'mississippi'.split('iss')
>>> a
['m', '', 'ippi']
>>> b = 'iss'.join(a)
>>> b

Every element in the split list is a string, including the empty string we see above. That element is crucial if we are to re-join and restore the original.

I kinda understand what you are saying right now but I have no idea how it answers my question. What I gathered right now is that join and split remember what was the original state of a string that is operated on.

I really need to take it slow and in pieces to understand some concepts so I really need to know if my previous assesment is correct in a “yes” or “no” kind of answer, otherwise I have a problem with conecting what you are saying to the rest.

They don’t ‘remember’ anything. We do. We’re the one’s who decide what to split on, and if we join using the same separator we arrive back at the original.

We cannot split a number, such as, 1234567. We can only split strings, which is why we get a list of strings. A list consists of elements separated by commas.

>>> s = "A quick brown fox jumps over the lazy dog"
>>> t = s.split()    # same as `s.split(' ')`
>>> t
['A', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

When we go to join, we must remember to replace the space character that was removed in the split.

>>> u = ' '.join(t)
>>> u
'A quick brown fox jumps over the lazy dog'

join/split have specific behaviour, read what that is before you start speculating.

>>> help(''.join)
join(iterable, /) method of builtins.str instance
    Concatenate any number of strings.

    The string whose method is called is inserted in between each given string.
    The result is returned as a new string.

    Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'

>>> help(''.split)
Help on built-in function split:

split(sep=None, maxsplit=-1) method of builtins.str instance
    Return a list of the words in the string, using sep as the delimiter string.

      The delimiter according which to split the string.
      None (the default value) means split according to any whitespace,
      and discard empty strings from the result.
      Maximum number of splits to do.
      -1 (the default value) means no limit.

As you can see, str.join accepts an iterable of strings, so both your examples are the same thing and both of them behave the same.

But as you can see above they don’t. In one case I got strings glued together and in another I got them as a list, each separated with a coma and inside “”.

The thing you pasted is beyond my ability to comprehend right now, it’s very technical and I dont understand even first sentence “join(iterable, /) method of builtins.str instance”


which is it? string or list?

no such thing as “inside quotes”, you can’t put things in quotes, quotes isn’t a container, it’s not a list

If you look at the documentation, str.join returns a string, and str.split returns a list of strings

I mean something like this one

So … it did what it promised to do?

read it. it’s no more technical than what you need to know to use it.