Write a Python program to convert a string in a list

Python | Program to convert String to a List

In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this we use the split() method.
Syntax:

string.split("delimiter")

Examples:

Input : "Geeks for Geeks" Output : ['Geeks', 'for', 'Geeks'] Input : "Geeks-for-Geeks" Output : ['Geeks', 'for', 'Geeks']

The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
Example 1:




# Python code to convert string to list

def Convert(string):

li = list(string.split(" "))

return li

# Driver code

str1 = "Geeks for Geeks"

print(Convert(str1))

Output:

['Geeks', 'for', 'Geeks']

Example 2:






# Python code to convert string to list

def Convert(string):

li = list(string.split("-"))

return li

# Driver code

str1 = "Geeks-for-Geeks"

print(Convert(str1))

Output:

['Geeks', 'for', 'Geeks']

Example 3:




# Python code to convert string to list character-wise

def Convert(string):

list1=[]

list1[:0]=string

return list1

# Driver code

str1="ABCD"

print(Convert(str1))

Output:

['A','B','C','D']

Write a Python program to convert a string in a list




Article Tags :

Python

Python list-programs

Python string-programs

python-list

python-string

Practice Tags :

python-list

Convert string to list in Python

In this short tutorial, find how to convert string to list in Python. We look at all the ways you can achieve this along with their pros and cons.

Introduction

While programming we may need to convert a string to list in Python. That could be for any other reason. But, a question arises here, how can we convert a string to different forms of lists?

So, here in this tutorial, we are going to learn how we can convert a string into a list in Python.


Python: Convert a string to a list

Last update on June 26 2021 10:22:07 (UTC/GMT +8 hours)

Python: Convert a given string into a list of words

Last update on June 26 2021 10:22:20 (UTC/GMT +8 hours)

How to Convert String to List in Python

The data type conversion is the most common operation in the Python language. In this tutorial, we will see How To Convert Python String to List and List to String. Then, we will try to convert the given string to a list, where spaces or any other special characters.

Method 1: Convert String to list of strings in Python

Syntax:

string.split(separator, maxsplit)

Parameters:

  • Separator: separator to use when splitting the string
    • Default value: whitespace
  • maxsplit: number of splits required

str1 = "Python pool for python knowledge" list1 = list(str1.split(" ")) print(list1)

Output:

['Python', 'pool', 'for', 'python', 'knowledge']

The split method by default takes whitespace as delimiter and separates the words of the string from by the whitespace and converts them into a list.

Method 2: Convert String to list of characters in Python

To convert a string into list of characters, we can simply use type conversion using the inbuilt list() method.

Syntax:

list(iterable)

Parameter:

  • iterable: an object that could be a sequence/collection/any iterator object

str1 = "Python pool for python knowledge" list1 = list(str1) print(list1)

Output:

['P', 'y', 't', 'h', 'o', 'n', ' ', 'p', 'o', 'o', 'l', ' ', 'f', 'o', 'r', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ', 'k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e']

Using type conversion with the help of list() method, it directly converts the given string into a list of characters for us.

Python Convert String to List

Let’s look at a simple example where we want to convert a string to list of words i.e. split it with the separator as white spaces.

s = 'Welcome To JournalDev' print(f'List of Words ={s.split()}')

Output: List of Words =['Welcome', 'To', 'JournalDev']

If you are not familiar with f-prefixed string formatting, please read f-strings in Python

If we want to split a string to list based on whitespaces, then we don’t need to provide any separator to the split() function. Also, any leading and trailing whitespaces are trimmed before the string is split into a list of words. So the output will remain same for string s = ' Welcome To JournalDev ' too.

Let’s look at another example where we have CSV data into a string and we will convert it to the list of items.

s = 'Apple,Mango,Banana' print(f'List of Items in CSV ={s.split(",")}')

Output: List of Items in CSV =['Apple', 'Mango', 'Banana']