The split() function in Python programming language breaks the string on the basis of:
Answers
I would say that this is a really good question! It's been a while since I used this but thanks for reminding me! Here is how split() word. Well, there is not much to say.
split() in python can take 0, 1 and 2 arguments.
When you just say string.split(), the string is going to be split where there is a space.
If you do not want the string to be split at the space especially when you dont have a space but you want to split it at the commans(,).
Let's say you have a string = 'hi,hello'
You want to split right at the comma.
You would use, string.split(','). You are going to want to specify where you would want to split it inside the brackets/parenthesis.
If you have a word like 'I#am#a#coder'. Then you would want to split it at the hashtag and in python, you would type:
string.split('#')
Again, this is how you can use two arguments inside the brackets.
Let's say we have a string as 'hi#bye#bye'
You would want to split right at the at the hashtag but you want only the 'hi' and not the rest.
You would say string.split('#',1) The second parameter says within how many of the hashtags would u split.
If you are splitting at the hashtag, the second argument defines how many '#' you would want to split. it is called as the max parameter.
Now, there is one crucial thing to take away from this. When you split something, it is always stored in a list.
For example, our string is 'hi,hello' and we want to split it at the ','. We would type, string.split(',') and the output would be:
['hi','hello']
This is all about the split() command in python.