Python Split String by Space


In this article, you are going to learn how Python split string by space. We will use different methods like split() and re.split() to split the string.

We can perform a bunch of operations after splitting the string. like getting the first element, getting the last element, etc.

We are going to use split() method a lot in this section, so first let's take a brief introduction of split() method in python.

    Table of Contents

  1. split() Method in Python
  2. Python Split String By Space into List
    1. Using split() method
    2. Using re.split() method
  3. split string by space and get first element
  4. split string by space and get last element
  5. Split String by space only n times
  6. Conclusion

split() Method in Python

The split() method is used to split a string into substrings if it finds one or more occurrences of the separator. The separator is a string that is used to split the string. The separator is not part of the result.

The split() method returns a list of strings after splitting the string.

Here is the syntax of the split() method in python.

str.split(separator, maxsplit)
  1. separator - It is a string that is used to split the string.
  2. maxsplit - It is the maximum number of splits to do. If maxsplit is omitted or -1 (the default), then there is no limit on the number of splits.

Example

str = "Python-problem - solving-is fun"

# split string at -
print(str.split("-"))

Output

['Python', 'problem ', ' solving', 'is fun']

Python Split String By Space Into List

There are 2 methods that can be used to split a string by space.

  1. Using split() method
  2. Using re.split() method

Let's see how to split a string by space using the split() method.

1. Using split() method

You have seen a brief introduction of the split() method above. Now we are going to use this method to split a string by space.

To split string by space pass space as a string to split() method as a separator. The method then returns a list of strings after splitting the string.

str = "Tutorials Tonight solving is fun"

# python split string by space
result = str.split(" ")

print(result)

In the above example method takes space as a separator and splits the string at each space and returns the result as a list of strings.

Output

['Python', 'problem', 'solving', 'is', 'fun']

One more thing to note is that if you do not pass any separator to split() method, it will split the string at each space or tab.

It is the same as python split string by space or tab.

str = "Tutorials Tonight      solving is        fun"

# split at space or tab
result = str.split()

Output

['Python', 'problem', 'solving', 'is', 'fun']

2. Using re.split() method

re is a module in python that is used to perform regular expression operations. It has a split() method that is used to split a string by regular expression.

The re.split() method can be used to split a string by space. It is similar to the split() method but it uses a regular expression to split the string.

Syntax

re.split(pattern, string, maxsplit)
  1. pattern - It is a regular expression that is used to split the string.
  2. string - It is the string that is used to split the string.
  3. maxsplit - The maximum number of splits to do. -1 (the default) means no limit.

Example

# import re module
import re

str = "Tutorials Tonight solving is fun"

# python split string by space
result = re.split(" ", str)

print(result)

Output

['Python', 'problem', 'solving', 'is', 'fun']

Python split string by space and get first element

Now let's see how to get the first element of the list that is returned after splitting the string by space.

We can see in the above examples that the return value is a list of strings. We can use that and get the first element by using list[0].

srt = "Tutorials Tonight solving is fun"

result = srt.split(" ")

# get first element
print(result[0])

The code above will print the first element of the list that is returned after splitting the string by space.

Output

Python

Python split string by space and get last element

The last element of the list can be accessed by using list[-1] or list[len(list) - 1] (length of string -1).

srt = "Tutorials Tonight solving is fun"

result = srt.split(" ")

# get last element
print(result[-1])

#or
print(result[len(result) - 1])

The above example will return the last element of the list generated by splitting the string by space.

Output

fun

Split String by space only n times

Sometimes you may not want to split the string by space but you want to split the string by space only n times. Means after n times of split you want to stop splitting the string.

In this case you can use maxsplit parameter in split() or re.split() method.

The maxsplit parameter is used to limit the number of splits to n. If you do not pass this parameter, it will split the string by space until it reaches the end of the string.

srt = "Tutorials Tonight solving is fun"
        
# split string by space only 2 times
result = srt.split(" ", 2)
print("split - ", result)

# or
import re
result = re.split(" ", srt, 2)
print("re.split - ",result)

Output

split - ['Python', 'problem', 'solving is fun']
re.split - ['Python', 'problem', 'solving is fun']

Conclusions

Summarising the above concepts, we can say that we can split a string by space or tab by using:

  1. split() method - Built-in method in python to split a string. Pass space or tab as a separator.
  2. re.split() method - Regular expression method to split a string.

For reference, you can visit python documentation for split() and re.split() methods.

Check out how to convert string to list in python.