Warning: session_start(): open(/tmp/sess_7d973f757c706e0a53647dd732a60cfa, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to split a string by unknown number of spaces in Python - LearnShareIT

How to split a string by unknown number of spaces in Python

Split a string by unknown number of spaces in Python

In Python, we have several functions that can be applied to perform string operations. This tutorial shows you how to split a string by unknown number of spaces in Python. We will guide you to use the split() method to do that. Check out the following information for detailed instructions.

Split a string by unknown number of spaces in Python 

To split a string by the unknown number of spaces in Python, you can apply the split() method in the following two ways:

Using the str.split() method

Call the split() method on your string object to split the string by spaces. 

Syntax:

string.split(separator, maxsplit)

Parameters: 

  • separator: Specify the separator element. Otherwise, It defaults to a whitespace.
  • maxsplit: Specify the maximum number of times to perform the split.

Note: Both of these parameters are optional.

Here is an example for you:

firstString = 'Welcome  to   LearnShareIT    community'
splitFirstString = firstString.split()
print(splitFirstString) 

Output:

['Welcome', 'to', 'LearnShareIT', 'community']

As a result, you can see that we have a list containing the words contained in the original string. They are separated by spaces in the original string and the split() method helps us to split them into substrings.

When you pass arguments to the split() method, you get different results. To illustrate, take a look at these examples:

Example using ‘maxsplit‘ argument:

firstString = 'Welcome to LearnShareIT community'

# Perform the split only 2 times
splitFirstString = firstString.split(" ",2)
print(splitFirstString) 

Output:

['Welcome', 'to', 'LearnShareIT community']

Example using ‘separator‘ argument:

secondString = 'Join-with-us'

# Prform a split every time a '-' character is encountered
splitSecondString = secondString.split('-')
print(splitSecondString) 

Output:

['Join', 'with', 'us']

Using the re.split() method

You can also perform splits using the split() method in combination with the regular expression. We need to use the ‘re’ module in Python with the import statement to do that.

You can see the example below:

import re

firstString = 'Welcome  to   LearnShareIT    community'

# Using regular expression
splitFirstString = re.split(r'\s+', firstString)
print(splitFirstString)

Output:

['Welcome', 'to', 'LearnShareIT', 'community']

In this example, the symbol ‘\s‘ means a space and the accompanying ‘+‘ signifies that there can be multiple spaces. As you can see we also get the same result as in the first method.

However, with this approach you will be able to multiply empty strings like the example below:

import re

firstString = ' Welcome  to   LearnShareIT    community '

# Using regular expression
splitFirstString = re.split(r'\s+', firstString)
print(splitFirstString)

Output:

['', 'Welcome', 'to', 'LearnShareIT', 'community', '']

The original string has spaces at the beginning and the end, so in the output, we get two empty strings.

You can use the filter() function to remove empty elements.

import re

firstString = ' Welcome  to   LearnShareIT    community '

# Remove empty elements
splitFirstString = filter(None,re.split(r'\s+', firstString))

splitFirstString = list(splitFirstString)
print(splitFirstString)

Output:

['Welcome', 'to', 'LearnShareIT', 'community']

Summary

So we have introduced you to two approaches on how to split a string by unknown number of spaces in Python using the split() method. Using the first method will be more straightforward, but you will need the second method in some cases. You can choose which method to use depending on the case.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *