Convert space delimited string to a list in Python

In this Python tutorial, we will learn how to convert a space delimited string to a list.

Table Of Contents

Convert Space delimited string to a list using split()

The split() function of string class in Python is used to separate the string into multiple substring based on the delimeter. By default, it separates the strings by space as delimeter and stores them into a list.

Syntax:

string1.split()

Where string1 is the input string.

Example-1:

Let’s consider a string with spaces- ” Welcome to thisPointer ” and convert it into a list.

# Consider the string
string1=" Welcome to thisPointer "

# Actual String
print(string1)

# convert to List
listOfStr = string1.split()

print(listOfStr)

Output:

 Welcome to thisPointer 
['Welcome', 'to', 'thisPointer']

So our final list contains strings with no delimiters/spaces.

Example-2:

Let’s consider a string with spaces- “Hello thisPointer Python Language” and convert it into a list.

# Consider the string
string1="Hello  thisPointer Python      Language"

# Actual String
print(string1)

# convert to List
listOfStr = string1.split()

print(listOfStr)

Output:

Hello  thisPointer Python      Language
['Hello', 'thisPointer', 'Python', 'Language']]

So our final list contains strings with no delimiters/spaces.

Convert Space delimited string to a list using split() with for loop

The split() function of string class in Python is used to separate the strings wherever space occurs. By default, it separates the strings and stores them into a list. So we can iterate the string using for loop with an iterator.

Syntax:

for ele in string1.split():
    list1.append(ele)

Where string1 is the input string and ele is the iterator.

Example-1:

Let’s consider a string with spaces- ” Welcome to thisPointer ” and convert it into the list.

# Consider the string
string1=" Welcome to thisPointer "

# Actual String
print(string1)

list1=[]

# convert to List
for ele in string1.split():
    list1.append(ele)

print(list1)

Output:

  Welcome to thisPointer 
['Welcome', 'to', 'thisPointer']

So our final list contains strings with no delimiters/spaces.

Example-2:

Let’s consider a string with spaces- “Hello thisPointer Python Language” and convert it into the list.

# Consider the string
string1="Hello  thisPointer Python      Language"

# Actual String
print(string1)

list1=[]

# convert to List
for ele in string1.split():
    list1.append(ele)

print(list1)

Output:

Hello  thisPointer Python      Language
['Hello', 'thisPointer', 'Python', 'Language']

So our final list contains strings with no delimiters/spaces.

Convert space delimited string to a list using split() with the strip()

The strip() method is used to remove the delimiter spaces from the string and split() in python is used to separate the strings wherever space occurs. By default, it separates the strings and stores them into a list. So first we will apply strip() and then we will use split(). Finally using the list() method, we will convert the result into the list.

Syntax:

list(string1.strip().split())

Example-1:

Let’s consider a string with spaces- ” Welcome to thisPointer ” and convert it into a list.

# Consider the string
string1=" Welcome to thisPointer "

# Actual String
print(string1)

# convert to List
listOfStr = list(string1.strip().split())

print(listOfStr)

Output:

  Welcome to thisPointer 
['Welcome', 'to', 'thisPointer']

So our final list contains strings with no delimiters/spaces.

Summary

In this article, we saw how to convert a space delimited string to a list in Python. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top