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.
In this article, we will discuss some programs to convert String to a List. So, Let's get started!
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.
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:
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:
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:
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))
a="Coding isfun" x=[i for a,i in enumerate(a)] print(x)
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:
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:
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:
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:
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)
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.
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.