Javaexercise.com

How to Convert List to String in Python

In this topic, we will learn to convert Python list to string. The list is a linear data structure that is used to store elements while the string is a sequence of characters enclosed within single or double-quotes.

Here, we have several examples to understand the conversion between list to string and vice versa.

To convert the list to a string, we used join() method that is a string class method and used to get a string from an iterable. In the second example, we are using map() method with join() to cast non-string elements in the list. If we do not use the map() method then we get an error at runtime due to string conversion.

Let's see examples to understand the conversion of list to string and vice versa.

Example: Python List to String Conversion using join() Method

Here, we are converting list to string type using the join() method. This method returns a string from the iterable: list, tuple, etc. Here, the type() method is used to check the type of value after conversion to ensure that conversion is successful.

# Take a list
list_val = ["python","is","easy","to","learn"]
print(list_val)
# Convert list to string
str_val = " ".join(list_val)
# Print value and it's type
print(str_val)
print(type(str_val))

Output:

['python', 'is', 'easy', 'to', 'learn']
python is easy to learn
<class 'str'>

List to String conversion using join() and map() Methods

If we have a list that contains non-string elements then we must use map() method, otherwise the join() method raises a TypeError. Here, we are using the map() method inside the join() method to avoid any type of error.

# Take a list
list_val = ["python", 3.8 ,"is","easy","to","learn"]
print(list_val)
# Convert list to string
str_val = " ".join(map(str,list_val))
# Print value and it's type
print(str_val)
print(type(str_val))

Output:

['python', 3.8, 'is', 'easy', 'to', 'learn']
python 3.8 is easy to learn
<class 'str'>

Python String to List Conversion using split() Method

After learning the conversion of the list to string. Now, let's learn conversion from string to list which is exactly the reverse process of the previous one. Here, we are using split() method of String class that returns a list of string.

# Take a string
str_val = "python is easy to learn"
print(str_val)
# Convert string to list
list_val = str_val.split(" ")
# Print value and it's type
print(list_val)
print(type(list_val))

Output:

python is easy to learn
['python', 'is', 'easy', 'to', 'learn']
<class 'list'>

Converting String to List in Python

Let's see one more way to convert string to list. Here, we are using list() function that returns a list from the specified type.

# Take a string
str_val = "python"
print(str_val)
# Convert string to list using list() function
list_val = list(str_val)
# Print value and it's type
print(list_val)
print(type(list_val))

Output:

python
['p', 'y', 't', 'h', 'o', 'n']
<class 'list'>

 


Conclusion

Well, in this topic, we learnt to convert list to string and vice versa. We used join() and map() functions to convert list to string and split() & list() methods to convert string to list type.

If we missed something, you can suggest us at - info.javaexercise@gmail.com