Python Program To Convert An Array List Into A String And Viceversa


Converting a list into a string

One method for converting a list into a string is to iterate through all the items of a 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 cheese cake 

Using Join Function

Join is an in-built function in python which is used for joining the items of an iterable object (ex: list) using a separator which will be specified by the user. We can use the join function for converting a list into a string but all the elements in the list should be strings and not of a different type.

If the list contains elements of any other data type, then we need to first convert the elements into string datatype using the str() function and then we can use the join function.

Syntax

The syntax for the join function is −

S=” “.join(L)

Where,

  • S=string that you get after joining.

  • L=The iterable object.

The separator should be specified within the double quotations.

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 cheese cake
I*want*cheese*cake
I@want@cheese@cake

The above example gives a type error because all the items in the list are not of the type “string”. So, in order to get the correct output, the list items 2 and 3 should be converted into 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

Converting A String Into A List

The split() function can be used for converting a string into a list. The split() function takes the string as an input and produces a list as an output based on the separator(separator means the character based on which the string will be split) mentioned. If separator is not mentioned, then by default blank space will be considered as the separator.

Syntax

L=S.split()

Where.

  • S= The string that is being split.

  • L=The list that you get after splitting.

If you want to use any other separator other than blank space, you have to mention it within the parenthesis inside double quotations.

Ex: L=S.split(“#”)

Example

Following is another example where we are Not mentioning any separator –

s="let the matriarchy begin"
print("the string is:  ",s)
lis=s.split()
print("the list is: ",lis)

Output

Following is the output of the program.

the string is:   let the matriarchy begin
the list is:  ['let', 'the', 'matriarchy', 'begin']

Using the separator “,”

s="My favourite dishes are Dosa, Burgers, ice creams,waffles"
print("the string is:  ",s)
lis=s.split(",")
print("the list is: ",lis)

Output

Following is the output of the program.

the string is:   My favourite dishes are Dosa, Burgers, ice creams,waffles
the list is:  ['My favourite dishes are Dosa', ' Burgers', ' ice creams', 'waffles']

Using the separator “@”

s="Chrisevans@gmail.com"
print("the string is:  ",s)
lis=s.split("@")
print("the list is: ",lis)

Output

Following is the output of the program.

the string is:   Chrisevans@gmail.com
the list is:  ['Chrisevans', 'gmail.com']

Updated on: 24-Apr-2023

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements