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.

Advertisements

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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top