Python split string by space

In this Python tutorial, we will learn how to split the string by space in Python. With the Python split function, we will cover these topics.

  • Python split string by space into list
  • Python split string by space and get first element
  • Python split string by space and comma
  • Python split string by space but not inside quotes
  • Python split string by space and newline
  • Python split string by space and get last element
  • Python split string without whitespace
  • Python regex split string by space

Python split string by whitespace

  • In this section, we will discuss how to split the string by whitespace in Python.
  • To do this task, we are going to use the Python string split() method. In Python, this function is used to split the string from the given separator and always returns a list of all words.
  • In this method by default, the separator is whitespace and it is an inbuilt function in Python.

Syntax:

Let’s have a look at the Syntax and understand the working of the Python split() function

str.split
         (
          separator,
          maxsplit
         )
  • It consists of a few parameters
    • separator: This parameter indicates the separator and by default any whitespace is a separator.
    • maxsplit: It is an optional parameter and indicates the maximum number of splits.

Example:

new_string="John is a good boy"

result=new_string.split()
print(result)

In the following given code, we have used the whitespace as a delimiter. To do this task we have an input string and by using the string.split() method we can easily separate the string.

Here is the implementation of the following given code

Python split string by whitespace
Python split string by whitespace

Also, check: Python download and Installation steps

Python split string by space into list

  • In this program, we will discuss how to split the string by space using the list constructor.
  • To do this task first we will create an input string and then generate a list constructor. In Python, this method is used to return a list and within this constructor, we have passed the input string.

Syntax:

Here is the Syntax of Python list() constructor

list(iterable)

Example:

new_str = 'Micheal'

result = list(new_str)
print("split strings",result)   

Here is the execution of the following given code

Python split string by space into list
Python split string by space into a list

Read: How to convert list to string in Python

Python split string by space and get first element

  • In this section, we will discuss how to split the string by space and get the first element in Python.
  • To do this task first we will create a string that contains integer values separated by space. Now we are going to use the str.split() function and split the string along with that we will use the [0] indexing. It will display the first element of splitting the string.

Example:

new_string="23 24 26"

result=new_string.split()[0]
print(result)

Here is the Screenshot of the following given code

Python split string by space and get first element
Python split string by space and get the first element

Read: Python find number in String

Python split string by space and comma

  • In this program, we will learn how to split the string by space and comma in Python.
  • In this example, we are going to use the comma separator in the split() function for splitting the string by space and comma. Once you will print ‘new_result’ then the output will display the substring of the string.

Example:

new_string = ' Germany, France, England '

new_result = new_string.split(',')
print(new_result)

You can refer to the below Screenshot

Python split string by space and comma
Python split string by space and comma

Read: Remove character from string Python

Python split string by space but not inside quotes

  • In this section, we will discuss how to split the string by space but not inside quotes in Python.
  • To perform this particular task we are going to use the shlex module and it is used to split the string and write in lexical analyzers.

Example:

import shlex

new_string=('China, England, "France Malaysia"')
new_output=shlex.split(new_string)
print(new_output)

Here is the Screenshot of the following given code

Python split string by space but not inside quotes
Python split string by space but not inside quotes

Read: Python 3 string replace() method

Python split string by space and newline

  • In this section, we will discuss how to split the string by space and newline in Python.
  • In this example, we have just created a simple string that contains ‘Country name’ in single-quoted but inside the string, we have used doubled quoted in “France Malaysia”, Once you will execute the below code the output displays ‘France Malaysia’ is a separated string.
  • Now we will decalre a variable ‘new_result’ and assign split() function. Once you will print ‘new_result’ then the output will display the list in which substrings have been stored.

Example:

str_value = 'Japan\nRussia England' 

new_result = str_value.split('\n')

print("split strings:",new_result)

Here is the implementation of the following given code

Python split string by space and newline
Python split string by space and newline

Read: Python 3 string methods

Python split string by space and get last element

  • In this section, we will discuss how to split the string by space and get the last element in Python.
  • To perform this particular task first we will create a string that contains integer values separated by space. Now we are going to use the str.split() function and split the string along with that we will use the [-1] indexing. It will display the last element of splitting the string.

Source Code:

new_str_values="56 83 178"

new_output=new_str_values.split()[-1]
print(new_output)

Here is the output of the following given code

Python split string by space and get last element
Python split string by space and get the last element

Read: Python find substring in string

Python split string without whitespace

  • In this section, we will discuss how to split the string without whitespace in Python.
  • In this example, we will not use the whitespace delimiter in the split() function. we are going to use the ‘*’ delimiter. Once you will print ‘result’ then the output displays the multiple strings.

Example:

new_string="John*is a good boy"

result=new_string.split('*')
print(result)

Here is the implementation of the following given code

Python split string without whitespace
Python split string without whitespace

Read: Slicing string in Python

Python regex split string by space

  • In this Program, we will discuss how to split the string by using the regex module in Python.
  • To do this task first we will import the re module and then initialize a string. Next, we will declare a variable ‘new_result’ and assing the.split() function. Within this function, we have passed the ‘+’ delimiter as an argument that indicates the string will be divided into substrings.

Example:

import re 

string_value= "john micheal  George Chris"         
new_result= re.split(' +',string_value)
print(new_result)

Here is the implementation of the following given code

Python regex split string by space
Python regex split string by space

You may also like to read the following Python tutorial.

In this Python tutorial, we have learned how to split the string by whitespace in Python. Moreover, using the Python split function, we have covered these topics.

  • Python split string by space into list
  • Python split string by space and get first element
  • Python split string by space and comma
  • Python split string by space but not inside quotes
  • Python split string by space and newline
  • Python split string by space and get last element
  • Python split string without whitespace
  • Python regex split string by space