Searched Python list to string? Want to convert list to string in Python? In this quick Python tutorial, you will learn how to turn a list into a string.
How to Convert List to String Python?
A list in python is a built-in data type and it’s like an array that contains similar data types values as well as different data types values.
Here are the requirements is to convert list elements to a single string.
Let see with a coding example.
Given Task:
input: ['Learn', 'coding ','from','LetsTacle']
without space between strings
output: "learncodingfromLetsTacle"
with delimiter such as space,#,!,@,* etc between strings
output: "Learn coding from LetsTacle"
output: "Learn#coding#from#LetsTacle"
For this, we can make use of multiple methods in python.
Method 1: Simple Concatenation | Python List to String
Let’s start with the very basic method of concatenation using a for loop for iterating list elements one by one and concatenating them to the result string, here our required result is in string str1.
#method 1 simple concatenation
input_list=[ 'learn', 'coding','from','LetsTacle']
str1 = ""
for val in input_list:
str1 += val
print(str1)
Output:
learncodingfromLetsTacle
Method 2: Using .join() Method Convert List to String
The join() method takes all items in an iterable and joins them into one string.
#method 2 Using .join() method
input_list=[ 'learn', 'coding','from','LetsTacle']
str2=""
print(str2.join(input_list))
Output:
learncodingfromLetsTacle
Method 3: Using map() to Turn a List Into a String | Python
Here we have used the inbuilt map() function, map() loops over the items of an input iterable and returns an iterator that results from applying a transformation function to every item in the original input iterable.
For example, below is the syntax for map() in Python:
map(function, iterable[, iterable1, iterable2,..., iterableN])
In our case, iterable is the input_list and we are converting each element to string type by str function.
#method 3 Using map()
input_list=[ 'learn', 'coding','from','LetsTacle']
str3 = ''.join(map(str,input_list))
print(str3)
Output:
learncodingfromLetsTacle
Method 4: Using list Comprehension | Python List to String
List comprehensions are used for creating new lists from other iterable like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.
In our case, iterable is input_list
Syntax for – List Comprehension
newList=[function/expression(element) for element in input_list if condition]
Here we are iterating element by element and converting them to string and saving in the list, at the end we have used join() on a new list to result in the required output string.
#method 4 Using list comprehension
input_list=[ 'learn', 'coding','from','LetsTacle']
str4 = ''.join([str(val) for val in input_list])
print(str4)
Output:
learncodingfromLetsTacle
Note: What if we want some delimiter in between the string while the list gets converted. Delimiter such as space, #,! @, * etc between strings
Example 1: For space in between the String
input_list=[ 'learn', 'coding','from','LetsTacle']
str1 = ""
#adding space delimiter before concatination
for val in input_list:
str1 += " "+val
print(str1)
Output:
learn coding from LetsTacle
Example 2: For * in between the String
input_list=[ 'learn', 'coding','from','LetsTacle']
str2="*" #give delimiter such as space,#,!,@, etc
print(str2.join(input_list))
Output:
learn*coding*from*LetsTacle
Example 3: For @ in between the String
input_list=[ 'learn', 'coding','from','LetsTacle']
#give delimiter such as space,#,!,@,* etc
str3 = '@'.join(map(str,input_list))
print(str3)
Output:
learn@coding@from@LetsTacle
You can use any of the methods shown and use your desired delimiter in between the string.
Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed