Convert List To String In Python

Published On - 27 Mar 2024

In this article we will show you the solution of convert list to string in python, in Python, we can use various approaches that convert lists to strings such as using for loop, reduce(), and list comprehension.

These approaches are really helpful to convert lists to strings in Python.

As we all know for loop iterates each list of elements using a separator to convert list to string.

Using reduce(), we can pass lambda expression that converts a list to a string, and list comprehension is the efficient way that takes less space as compared to loops.

Step By Step Guide On Convert List To String In Python :-

1. Using for loop

def convert(a):
    s=""
    for i in a:
        s=s+i
    return s
a=input().split()
print(convert(a))
#input: Talkers code
#output: Talkerscode
  1. In this code example, we create a function to convert a list to a string.
  2. We create an empty string s.
  3. Then we can use for loop to iterate each element present in the list of strings.
  4. Then we can add a separator with i so that the list brackets will remove and convert into strings.
  5. We assign a variable a which takes input that represents a list of strings.
  6. Then we can call convert() function to gives output.
a=["Hindi", "English", "Sanskrit"]
s=""
for i in a:
    print(i,end=" ") #Hindi English Sanskrit
  1. We initialize a variable a and assign the list of strings.
  2. We assign an empty string s.
  3. Then we can use for loop to iterate each element in the list.
  4. After that we can print the i and use the end operator so that space is created in each word.

2. Using reduce()

from functools import reduce
a=["Hindi", "English", "Sanskrit"]
b=reduce(lambda x,y: x+ " " + y, a)
print(b)
  1. Firstly, we can import reduce from functools.
  2. Then we assign a variable a that represents the lists of strings.
  3. We can use reduce() where we can pass a lambda expression that represents a single statement.
from functools import reduce
def convert(a):
    b=reduce(lambda x,y: x+ " " + y, a)
    return b
a=input().split()
print(convert(a))
  1. Firstly, we can import reduce from functools.
  2. Then we create a function named convert.
  3. We can use reduce() where we can pass a lambda expression that represents a single statement.
  4. We assign a variable that takes input from the user.
  5. Then we can call convert() function to gives output.

3. Using list comprehension

a=["Hindi", "English", "Sanskrit"]
b= " ".join([str(i) for i in a])
print(b)
  1. We initialize a variable that represents the lists of strings.
  2. Then we can use list comprehension to iterate each element in the list and then convert it into a string using the join() method indicates that the list convert into a string.
a=input().split()
b= " ".join([str(i) for i in a])
print(b)
  1. We assign a variable a that takes input from the user which represents a list of strings.
  2. We can use list comprehension to iterate each element in the list and then convert it into a string using the join() method indicates that the list convert into a string.

Conclusion :-

In conclusion, we successfully learned how to convert a list to a string.

We learned various approaches such as for loop, reduce, and list comprehension. We also learned various code examples that explained the use to convert a list to a string.

I hope this article on convert list to string in python helps you and the steps and method mentioned above are easy to follow and implement.