How to split the python string that gives you individual list of words

How to split the python string that gives you individual list of words

Vipul kunwar's photo
Vipul kunwar
·Nov 12, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article


Do you know you can split the python string?

Yes, you've heard the right.

You can grab the individual word from the string.

But wait a minute, how it's possible?

Simply, It's possible!!!

In this blog post I'm going to show you how It's possible. * That means I'm going to split the python string.*

Also, You can replace the word and grab the new one.

## To do this use the split() method

First of all, you need a method to do it.

For this, you're going to use split() method.

Second, it has syntax string.split(seperator, max_split).

That means you can perform this operation using .(dot) operator.

  • The seperator seperates the string using symbols*.

  • A max_split showing, how many times you want to split the string.

## Return value of split():

So, you've seen the syntax of split().

Now, it's time to see the return value of it.

First of all, I'm going to return the specific word list.**

The data type of specific word is string.

But they are individual.

That means you can perform methods differently on each one.

For example,

Screenshot 2021-11-11 at 11-11-41 Carbon.png

Screenshot 2021-11-11 at 11-17-42 Carbon.png

Here, you can see that string has splitted.

Also, you can see that whitespace has splitted the string.

Let's say I don't want to split it with a whitespace.

So, you can use string.split(seperator).

For instance, I'm going to split the string with # symbol.

The code look like:

Screenshot 2021-11-11 at 11-25-49 Carbon.png

After, running the above code you'll see --->

Screenshot 2021-11-11 at 11-27-46 Carbon.png

From here, you can see that you can seperate the string with any symbol.

Likewise, you can use max_split parameter.

This parameter shows how many times you've to split the string.

The code look like

Screenshot 2021-11-12 at 08-31-03 Carbon.png

After running the code--->

Screenshot 2021-11-12 at 08-33-20 Carbon.png

So, you can observe that string has splitted one time.

Likewise, here's the list of two words.

Also, you can split the string any number of times with max_split.

At last, you've learnt the split() method.

## Conclusion

Finally, you've come to an end of this blog post.

You've seen the steps to split the python string.

Also, you can split the string with any seperator such as $,#,@ and many more.

Likewise, you can use max_split parameter to define the number of times you want to split the string.

The return value of split() method is list of words.

That means every word has individuality. As you like to be individual.

You can perform different methods on this list.

So, what do you think about it? Is there anything you want to cover? Comment on it. If you'd liked, please share it.

Also, subscribe to my newsletter. so, you can grab the fresh python topics.

And the last one. Do I've written good copy for you?

Did you find this article valuable?

Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this