What does the delimiter do?

Does the argument “delimiter”, in the example, do something specific, or was it just a convenient word to keep the argument field from being empty?

Yes, it describes the string to split on, whether a single character or multiple characters. The default form of the method takes no argument and splits on space characters. We cannot split on an empty string.

str.split()        # split on spaces
str.split('c')     # split on 'c'
str.split("")      # syntax error - empty delimiter
23 Likes

perfectly valid syntax

2 Likes
>>> 'abcdefg'.split('')

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    'abcdefg'.split('')
ValueError: empty separator
>>> 

To split on an empty string, use list(str)

4 Likes

It’s valid python code, it’s not valid input for that method.

1 Like

Thanks, I don’t think I fully understood what the word “delimiter” means. Not sure if it was defined earlier in the lessons, but after a Google search, I think I understand the concept behind it.

Your list helps me understand!

PS (Still new to all of this, so I apologize if I’m asking questions that seem obvious.)

4 Likes

D’oh! Not a syntax error. Sheesh.

I’m also still pretty new to this, and obviously this is months later.

But to my understanding the “delimiter” is basically the indicator of where you want it to be split.

so if you only put .split() it defaults to the white spaces.
or if you put .split(a) it will split at the ‘a’, which means ‘a’ is what is considered the delimiter.

3 Likes

A delimiter indicates an endpoint.

"a string"

'a string'

The quotes are delimiters.

A separator indicates what to split on.

>>> a = "*".join('mississippi')
>>> a
m*i*s*s*i*s*s*i*p*p*i
>>> b = a.split('*')
>>> b
mississippi
10 Likes

Correct me if i’m wong, but wouldn’t the last output in your message be a list?

[‘m’, ‘i’, ‘s’, ‘s’, ‘i’, ‘s’, ‘s’, ‘i’, ‘p’, ‘p’, ‘i’]

1 Like

No, you’re not wrong. I left out a step, the ''.join(b) part.

1 Like

Ah, thank you! I should’ve posted the solution of ’ '.join(b) myself, could’ve helped others.

Hi, ‘delimiter’ is a placeholder here. So, the meaning of ‘delimiter’ is a separator. You don’t actually type the word “delimiter”, you use whatever delimiter (separator) you want the split function to cut on.

You can tell it to split the string at a comma, a space, etc.

e.g.
random_string = " one, two and three, four"
split_random_string = random_string.split(",")

would print: [‘one’, ‘two and three’, ‘four’]
because it split the string at the ‘delimiter’ that I specified - the comma

6 Likes

To clarify,

delimiter  =>  separator string  =>  "..."

where ... is the character string.

1 Like

@mtf hate to necro this thread, but care to clarify:

:sweat_smile:

A delimiter is a boundary region (a character or set of characters) that is not included in the returned list.

a = "aaa@@@bbb@@@ccc@@@ddd"
b = a.split('@@@')
print (b)
['aaa', 'bbb', 'ccc', 'ddd']

In the above, @@@ is the delimiter.

Sorry, I should clarify - I understand what delimiter means.

I just want to know what the delimiter to separator string to … is suppose to represent

The separator string IS a delimiter. It is what keeps the parts we want separate.

1 Like