Python program to convert array list to string and vice versa
Python Program to Convert an Array List to a String and Back
Converting a List to a String
One way to convert a list to a string is to iterate over all the items in the list and concatenate them into an empty string.
Example
lis=["I","want","cheese","cake"]
str=""
for i in lis:
str=str+i
str=str+" "
print(str)
Output
I want cheesecake
Using the Join Function
Join is a built-in Python function that concatenates items of an iterable object (e.g., a list) using a user-specified delimiter. We can use the join function to convert a list to a string, but all elements in the list must be strings, not other types.
If the list contains elements of any other data type, you need to use the str() function to convert the elements to a string data type before using the join function.
Syntax
The syntax of the join function is:
S=" ".join(L)
Where,
- S=the string to be concatenated.
-
L=an iterable object.
The delimiter should be specified within double quotes.
Example
lis=["I","want","cheese","cake"]
print("the list is ",lis)
str1=" ".join(lis)
str2="*".join(lis)
str3="@".join(lis)
print(str1)
print(str2)
print(str3)
Output
the list is ['I', 'want', 'cheese', 'cake']
I want cheesecake
I*want*cheese*cake
I@want@cheese@cake
The above example will generate a type error because all items in the list are not of type “string”. Therefore, to obtain the correct output, list items 2 and 3 should be converted to strings.
lis=[“I”,2,3,”want”,”cheese”,”cake”]
print(“the list is “,lis)
str1=” “.join(str(i) for i in lis)
print(“The string is “,str1)
Output
the list is ['I', 2, 3, 'want', 'cheese', 'cake']
The string is I 2 3 want cheese cake
<h2>Converting a String to a List</h2>
<p>You can use the split() function to convert a string to a list. The split() function takes a string as input and produces a list as output based on the delimiter (the delimiter is the character that the string will be split on). If no delimiter is mentioned, spaces are included by default. </p>
<h3>Syntax</h3>
<pre><code class="language-python line-numbers">L=S.split()
Where.
- S=the string being split.
-
L=the list obtained after splitting.
If you want to use any delimiter other than spaces, you must specify it within brackets within double quotes.
Ex: L=S.split(“#”)
Example
Here is another example where we will not mention any delimiters –
s="let the matriarchy begin"
print("the string is: ",s)
lis=s.split()
print("the list is: ",lis)
Output
The following is the output of the program.
the string is: let the matriarchy begin
the list is: ['let', 'the', 'matriarchy', 'begin']
Using the delimiter “,”
s="My favorite dishes are Dosa, Burgers, ice creams, waffles"
print("the string is: ",s)
lis=s.split(",")
print("the list is: ",lis)
Output
The following is the output of the program.
the string is: My favorite dishes are Dosa, Burgers, ice creams,waffles
the list is: ['My favorite dishes are Dosa', ' Burgers', ' ice creams', 'waffles']
Use delimiter “@”
s="Chrisevans@gmail.com"
print("the string is: ",s)
lis=s.split("@")
print("the list is: ",lis)
Output
The following is the output of the program.
the string is: Chrisevans@gmail.com
the list is: ['Chrisevans', 'gmail.com']