How to split a String by WhiteSpace in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to split a String by WhiteSpace in Python. By spliting a string you can got the array of the string. Here we will learn how we can split a string by whitespaces. So there are some methods which can be use for it. So lets learn this by given methods.

How to split a String by WhiteSpace in Python

Advertisements
  1. Split a String by WhiteSpace in Python

    to split a String by WhiteSpace in Python just Use split(). By using split() function you can split a string by whitespaces. It will return us the array of the substrings. Here i am using a string “Hello guys. Good morning.” So by using split() function it will give us the array of the substring. Here the delimiter is whitespace. split() function cut the strings into substrings in the basis of whitespaces. So lets learn this by given example: mystr = 'Hello guys. Good morning.'.split() print(mystr) Output :
    ['Hello', 'guys.', 'Good', 'morning.']The split() function also use for to remove the leading and trailing white spaces and give split your string into substrings. So lets learn this : mystr = ' Hello guys. Good morning. '.split() print(mystr)
    Output : ['Hello', 'guys.', 'Good', 'morning.'] split() function can also remove the \n , \t , \r from the whitespace litral. In your string there are some tab than that will removed by split() function and it will give you the sub strings. Lets see this by example: mystr = ' Hello \n guys.\r Good\t morning. '.split() print(mystr)
    Output : ['Hollo', 'guys.', 'Good', 'morning.']

  2. How to split a String by WhiteSpace in Python

    to split a String by WhiteSpace in Python just Use re.split(). By using re.split() you can split a String by WhiteSpace in Python. So lets learn this method by given below example: import re mystr = 'Hello \n guys.\r Good\t morning.' print(re.split(r'\s+', mystr)) Output : ['Hello', 'guys.', 'Good', 'morning.']

Method 1: Use split()

Advertisements

By using split() function you can split a string by whitespaces. It will return us the array of the substrings. Here i am using a string “Hello guys. Good morning.” So by using split() function it will give us the array of the substring. Here the delimiter is whitespace. split() function cut the strings into substrings in the basis of whitespaces. So lets learn this by given example:

mystr = 'Hello guys. Good morning.'.split()
print(mystr)

Output :

['Hello', 'guys.', 'Good', 'morning.']

The split() function also use for to remove the leading and trailing white spaces and give split your string into substrings. So lets learn this :

mystr = '   Hello  guys. Good morning.  '.split()
print(mystr)

Output :

['Hello', 'guys.', 'Good', 'morning.']

split() function can also remove the \n , \t , \r from the whitespace litral. In your string there are some tab than that will removed by split() function and it will give you the sub strings. Lets see this by example:

mystr = '   Hello \n guys.\r Good\t morning.  '.split()
print(mystr)

Output :

['Hollo', 'guys.', 'Good', 'morning.']

Method 2: Use re.split()

Advertisements

By using re.split() you can split a String by WhiteSpace in Python. So lets learn this method by given below example:

import re
mystr = 'Hello \n guys.\r Good\t morning.'
print(re.split(r'\s+', mystr))

Output :

['Hello', 'guys.', 'Good', 'morning.']

Method 3: Use re.findall()

Advertisements

In this method you have to change the whitespace latter \s by capitalizing it like\S. THis will help you to split a string by whitespaces in python. So lets learn this by given below examples.

import re
mystr = 'Hello \n guys.\t Good\r morning.'
print(re.findall(r'\S+', mystr))

Output :

['Hello', 'guys.', 'Good', 'morning.']

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment