Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What Exactly are Lists?
3.
What does "Strings" mean?
4.
Converting string to list in Python
4.1.
Method 1: Using split()
4.2.
Python
4.3.
Python
4.4.
Python
4.5.
Method 2: Using Slicing
4.6.
Python
4.7.
Method 3: map() 
4.8.
Python
4.9.
Method 4: Enumerate Function 
4.10.
Python
4.11.
Python
4.12.
Method 5: list()
4.13.
Python
4.14.
Method 6: JSON
4.15.
Python
4.16.
Method 7: Using strip()
4.17.
Python
4.18.
Method 8: lambda Function
4.19.
Python
4.20.
Method 9: Combination
4.21.
Python
5.
Frequently Asked Questions
5.1.
What does a Python string mean?
5.2.
How do you convert a string back to a list in Python?
5.3.
How do I convert a string to a list without splitting in Python?
5.4.
How to convert string to list array in Python?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Program to Convert String to a List

Introduction

In Python, a string is a sequence of characters enclosed within single (' '), double (" "), or triple (''' ''' or """ """) quotes. Strings are immutable, meaning their values cannot be changed after creation. While a list is a versatile, ordered collection of elements in Python. Lists are mutable, allowing modifications like appending, inserting, or removing elements. Converting a string to a list is beneficial in scenarios where individual characters or substrings need separate manipulation. 

If you're into data research, machine learning, or web development, Python is your language.

convert string to list python

In this article, we will discuss some programs to convert String to a List. So, Let's get started!

Also see, Python Filter Function

What Exactly are Lists?

A group of arranged items is referred to as a list. Strings, integers, and other types of data types, as well as other lists, can all be included in lists. The list is indexed, with 0 being the initial index, and each entry is given an index according to a specific order.

Because lists may be altered even after they are created, they are considered changeable objects. The entries on lists may be identical.

list =["Dhiraj",17,["Students","in","class"]]

What does "Strings" mean?

A string is a group of characters that are arranged in sequential order. Python comes with a class called "str" specifically designed to handle strings. They can be enclosed in a single, double, or triple inverted comma. 

fname='''Mira '''
lname='Khewal'

Converting string to list in Python

It is relatively common in Python to convert data types for any reason it might be. Python does not make it easy to convert string to list in Python, though.

To create lists from strings, use the list function (). This technique, however, would return a list of characters since Python would not be able to tell where each item begins and finishes. As a result, Python has a few different techniques that may be used to change a string into a list per our needs.

convert string to list python

Method 1: Using split()

A string can be divided using the split method according to a delimiter that has been given in converting string to list in Python. After splitting, it returns the separated string as a list.

The strings are divided into smaller chunks and put into the list using the split function. The built-in method uses the "delimiter" as the delimiter string and provides a list of the words that make up the string. A separate splitting mechanism is used if a delimiter is not supplied or is None. 

Runs of consecutive whitespace are treated as a single separator, and the output will not include any empty strings at the start or end of the string comprising the leading or the following whitespace.

  • Python

Python

string = "Coding is fun"
list = string.split()
print(list)


Or

  • Python

Python

def Convert(string):
   list = list(string.split(" "))
   return list
string = "Coding is fun"
print(Convert(string))


Output: 

output

 

The string is divided on the space if the delimiter is given in place of the space between them.

  • Python

Python

string = "Coding|is|fun"
list = string.split("|")
print(list_1)


Output: 

['Coding', 'is', 'fun']


The method is the same as the previous program; the only distinction is that whenever a separator appears, one element from the list is taken.

Each word in a string is assigned to a different member of the list using the split() function, which returns a list. When separating a string according to a delimiter, the split() function comes in handy so that the string is separated and kept in a list. With the aid of a designated delimiter, the built-in method of Python is used to return a list of the words included in the string.

Method 2: Using Slicing

Using the slice syntax, you may return a selection of characters and convert the string to a list in Python. For a portion of the text to be returned as a list, specify the start index and the end index, each separated by a colon.

  • Python

Python

def Convert(string):
   list = [ ]
   list[:0] = string
   return list[5:12]
string = "Coding is fun"
print(Convert(string))


Output: 

output

Method 3: map() 

The map function requires two parameters, the first of which is the datatype you must supply for str in converting into a string, and the second is the list's name. Now, the map function first calls each element in an iterable sequence using the str() method, which turns each element into a string. The join function then creates a single string out of all the elements.

  • Python

Python

a="Coding is fun"
b=list(map(str,a))
print(b)


Output:

output

The functionality of the list has been mapped to each element of the string using the map() method in converting string to list in Python.

Method 4: Enumerate Function 

A built-in function called enumerate() produces an enumerate object in converting a string to a list in Python. You may iterate through a list and obtain the element's index.

  • Python

Python

a = "Coding is fun"
x = enumerate(a)
print(list(x))


Output:

[(0, 'C'), (1, 'o'), (2, 'd'), (3, 'i'), (4, 'n'), (5, 'g'), (6, ' '), (7, 'i'), (8, 's'), (9, ' '), (10, 'f'), (11, 'u'), (12, 'n')]

 

  • Python

Python

a="Coding isfun"
x=[i for a,i in enumerate(a)]
print(x)


Output:

output

Method 5: list()

One of the most popular methods in converting string to list in Python is the list(). Using this technique, a string is turned into a list of characters. Use this technique only when you are convinced that each character in the list belongs there and the string comprises a group of letters or integers that are not separated by spaces. Without such, spaces would likewise be regarded as characters and kept in a list.

  • Python

Python

a = "Coding is fun"
x = list(a)
print(x)


Output:

output

Method 6: JSON

Working with JSON data is made possible via the Python language's built-in module JSON. The json.loads() function allows you to parse a JSON string if you have one.

  • Python

Python

import json
a = '["Coding", "is", "fun"]'
res = json.loads(a)
print(res)


Output:

output

Method 7: Using strip()

Whenever you need to divide a string into a list of characters while you are converting a string to a list in Python, use this technique. Character division occurs in the list method for the string. A list variable holds the characters that make up the string in this instance. White space is regarded in the string as a single character, as is evident. To create a list where each character in the string corresponds to each entry in the list, we utilize the strip() function.

  • Python

Python

s = "Coding is fun"  
print(list(s.strip()))


Output:

output

Method 8: lambda Function

An anonymous, short function is known as a lambda. There is only one expression allowed for a lambda function, regardless of the number of arguments it accepts in converting string to list.

  • Python

Python

a="Coding is fun"
res=list(filter(lambda i:(i in a),a))
print(res)


Output:

output

Method 9: Combination

For variety in converting string to list in Python, combine any of the approaches mentioned earlier.

  • Python

Python

s="Coding is fun"
s=s.split()
res=list(map(list,s))
print(res)


Output:

output

You can compile it with an online python compiler.

Frequently Asked Questions

What does a Python string mean?

A collection of letters, words, or other characters is called a string. It is one of the basic data structures that serve as the foundation for manipulating data. The str class is a built-in string class in Python. Because Python strings are "immutable," they cannot be modified after they have been formed.

How do you convert a string back to a list in Python?

To convert a string back to a list in Python, you can use the list() function. For example: my_list = list("Hello") results in ['H', 'e', 'l', 'l', 'o'].

How do I convert a string to a list without splitting in Python?

You can use the list() function directly on the string. For instance, my_list = list("Python") creates ['P', 'y', 't', 'h', 'o', 'n'].

How to convert string to list array in Python?

To convert a string to a list array, apply the list() function on the string. Example: my_list = list("Data") yields ['D', 'a', 't', 'a'].

Conclusion

In this blog, we have discussed how we can convert string to list in Python. Firstly, we understood what list and string is. Then we have discussed several methods to convert along with the examples.

Recommended Readings:

Recommended problems -

If you think this blog has helped you enhance your knowledge about the above question, and if you want to learn more, check out our articles. And many more on our website.

Visit our website to read more such blogs. Make sure you enrol in the courses we provide, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

Live masterclass