Last modified: Jan 10, 2023 By Alexander Williams
How to Properly Convert a List to a String in Python
In this tutorial, we'll learn how to convert a list to a string by using two methods.
We'll learn also how to solve expected str instance, int found issue if you try to convert the list contains an integer.
Converting a list to string (method #1)
in the first method, we'll onvert alist to string without using any built-function.
let's see the example.
#list
my_list = ['Python', 'Django', 'Javascript', 'Nodejs']
my_string = ""
for i in my_list:
my_string += i
print(my_string)
let's explain.
1. declaring an empty variable.
2. iterating our list my_list.
3. adding the list's items to the empty variable my_list.
output:
To print the string with spaces, we need to use format format().
#list
my_list = ['Python', 'Django', 'Javascript', 'Nodejs']
my_string = ""
for i in my_list:
my_string += ('{} ').format(i)
print(my_string)
output:
Converting a list to string (method #2)
in this method #2, we'll use join() function.
syntax:
' '.join(tour_list)
to understand, let's see the following example.
#list
my_list = ['Python', 'Django', 'Javascript', 'Nodejs']
#convert list to str
string = ' '.join(my_list)
#print the result
print(string)
output:
But if your list contains one or more integers, does this method work?
to answer this question, let's try to convert a list that contains an integer.
#list
my_list = ['Python', 'Django', 2006, 'Javascript', 'Nodejs']
#convert list to str
string = ' '.join(my_list)
#print the result
print(string)
output:
mmmm! as we expected, but why?
Generally, in programming languages, we can't join or concatenate the data that have a different type of data.
in our list, we have two different data type.
int=2006.
str='Python', 'Django', 2006, 'Javascript', 'Nodejs'
to solve this issue, let's see how to convert a list contains an int to string.
Convert a list contains an int to string
as I said, when you try to convert a list that contains an integer, we'll get sequence item 2: expected str instance, int found.
To solve this issue, we need first convert the list's items to the string, then convert the list to string.
example:
my_list = ['Python', 'Django', 2006, 'Javascript', 'Nodejs']
new_list = [str(i) for i in my_list ]
#convert list to str
string = ' '.join(new_list)
#print the result
print(string)
output
Convert Two lists to a string
in this part of tutorial, we'll convert two lists to astring by using join().
my_list_1 = ['Python', 'Django', 'Javascript', 'Nodejs']
my_list_2 = ['Dog', 'Cat', 'Rat', 'Tiger']
#concatenate the lists
new_list = my_list_1 + my_list_2
#convert list to str
string = ' '.join(new_list)
#print the result
print(string)
output:
let me exmplain:
as you see in the example, we have 2 lists my_list_1 and my_list_2, and to convert these lists to a string we:
1. concaneting my_list_1 and my_list_2
2. converting the list to string by using join()
Conclusion
Today we learned how to convert a list to a string by using two methods.
We learned also how to solve sequence item 2: expected str instance, int found issue.