For Enquiry: 93450 45466

How To Convert List To String In Python?



You might have used lists in almost all of your programs, and suddenly required to change it to a string, maybe to print the result in a single line, or something else. So in this blog, I will share with you the various methods with which you can convert a list to a string in the following sequence.

Conversion using Iteration
Using join() method
Using str and strip method
With list comprehension
Using map() method
Converting String to list with list() and split() method

Converting List To String With Iterative Method

Here we will use for and while loops to iterate over the list and convert it to a string.

# Program to convert list to string using while loop


my_list = [‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, ‘FITA’]

my_string = ”

count = 0

while count < len(my_list):

      my_string += ‘ ‘+ my_list[count]

      count += 1

print(my_string)

# Program to convert list to string using for loop


my_list = [‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, ‘FITA’]

my_string = ”

for i in my_list:

     my_string += ‘ ‘+i

print(my_string)

And now this can be further simplified if you don’t want to store the list as a string

# Program to convert list to string using for loop

my_list = [‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, ‘FITA’]

for i in my_list:

    print(i,end=’ ‘)

All the above programs will have the same output as follows:

HI THERE YOU CAN JOIN WITH ME AT FITA

Notice when you add an integer to the my_list all the above programs will break, except if you convert the elements to a string.So try changing the elements to a string using str() method while concatenating just like this and you are good to go..

# Program to convert list to string using for loop

my_list = [‘HI’, ‘THERE’,’CAN’, ‘YOU’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’,2]


my_string = ”

for i in my_list:

   my_string += ‘ ‘+ str(i)

print(my_string)

Converting List To String With join() method

You can join the lists with any character in between using the ‘character’.join(iterator) method.Here is an example

# Program to convert list to string using join method


my_list = [‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, ‘FITA’]


my_string = ‘ ‘.join(my_list)

print(my_string)

Or like this if you don’t have to store the string.

# Program to convert list to string using join method


my_list = [‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, ‘FITA’]


print(‘ ‘.join(my_list))

Also you can directly pass the list to the join method as a parameter.

# Program to convert list to string using join method


print(‘ ‘.join([‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’,’FITA’]))

You can either pass a comma or something else within the quotations of the join method.Now for an integer in the list we might have to change the above program so that all the elements are of string data type.

# Program to convert list to string using join method


my_list = [‘HI’, ‘THERE’, ‘CAN’,’YOU’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, 2]


print(‘ ‘.join(str(i) for i in my_list))

Alternatively, you can just print the list as a string with str() method and remove out the [] with strip() method

# Program to convert list to string using str and strip method


my_list = [‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, 2]


print(str(my_list).strip(‘[]’))

Or by removing first and the last element

print(str(my_list)[1:-1])

Checkout this Python Online Course by FITA. FITA provides a complete Python course where you will be building real-time projects like Bitly and Twitter bundled with Django, placement support and certification at an affordable price.

Converting List To String With List Comprehension

Here is how you would join the list with list comprehension.

# Program to convert list to string using list comprehension


print(‘ ‘.join([str(i) for i in [‘HI’, ‘THERE’, ‘CAN’,’YOU’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, 2]]))

Convert list to string with map() method

The map method takes 2 positional arguments, a method and an iterable.a method could be an int() or str() and an iterable could be a list, tuple string, set etc.It will then map or change the elements of the iterable to with the method provided.

Here is an example for converting lists containing different data types to a string using map() and join() methods.

# Program to convert list to string using map and join method


my_list = [‘HI’, ‘THERE’, ‘CAN’, ‘YOU’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, 2]


print(‘ ‘.join(map(str, my_list)))


# or


print(‘ ‘.join(map(str, [‘HI’, ‘THERE’, ‘CAN’, ‘YOU’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, 2])))

Converting String to list

We have been converting list to strings, but you can also change string to list using the list() and split() method.

Here is an example for list() method.

# Program to convert string to list using list() method


my_list = ‘HI THERE YOU CAN JOIN WITH ME AT 2’


print(list(my_list))

which will output

[‘H’, ‘I’, ‘ ‘, ‘T’, ‘H’, ‘E’, ‘R’, ‘E’, ‘ ‘, ‘Y’, ‘O’, ‘U’, ‘ ‘, ‘C’, ‘A’, ‘N’, ‘ ‘, ‘J’, ‘O’, ‘I’, ‘N’, ‘ ‘, ‘W’, ‘I’, ‘T’, ‘H’, ‘ ‘, ‘M’, ‘E’, ‘ ‘, ‘A’, ‘T’, ‘ ‘, ‘2’]

But to change the string as we had earlier we can use the split() method.

# Program to convert string to list using split() method


my_list = ‘HI THERE YOU CAN JOIN WITH ME AT 2’


print(my_list.split(‘ ‘))

Output

[‘HI’, ‘THERE’, ‘YOU’, ‘CAN’, ‘JOIN’, ‘WITH’, ‘ME’, ‘AT’, ‘2’]

This was all about converting lists to string and strings to lists..To get in-depth knowledge of Python along with its various applications and real-time projects, you can enroll in Python Training in Chennai or Python Training in Bangalore by FITA Academy at an affordable price, which includes real time projects with certification, support and career guidance assistance.






Quick Enquiry

Please wait while submission in progress...


Contact Us

Chennai, Bangalore & Online

  93450 45466

Coimbatore

 95978 88270

Tirupur

99401 22502

Madurai

97900 94102

Pondicherry

93635 21112

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


FITA Academy Branches

Chennai

FITA Academy - Velachery
Plot No 7, 2nd floor,
Vadivelan Nagar,
Velachery Main Road,
Velachery, Chennai - 600042
Tamil Nadu

    :   93450 45466

FITA Academy - Anna Nagar
No 14, Block No, 338, 2nd Ave,
Anna Nagar,
Chennai 600 040, Tamil Nadu
Next to Santhosh Super Market

    :   93450 45466

FITA Academy - T Nagar
05, 5th Floor, Challa Mall,
T Nagar,
Chennai 600 017, Tamil Nadu
Opposite to Pondy Bazaar Globus

    :   93450 45466

FITA Academy - Tambaram
Nehru Nagar, Kadaperi,
GST Road, West Tambaram,
Chennai 600 045, Tamil Nadu
Opposite to Saravana Jewellers Near MEPZ

    :   93450 45466

FITA Academy - Thoraipakkam
5/350, Old Mahabalipuram Road,
Okkiyam Thoraipakkam,
Chennai 600 097, Tamil Nadu
Next to Cognizant Thoraipakkam Office
& Opposite to Nilgris Supermarket

    :   93450 45466

FITA Academy - Porur
17, Trunk Rd,
Porur
Chennai 600116, Tamil Nadu
Above Maharashtra Bank

    :   93450 45466

FITA Academy - Pallikaranai
335A, 13th Main Rd,
Ram Nagar South Extn,
Pallikaranai, Chennai,
Tamil Nadu 600100

    :   93450 45466

Bangalore

FITA Academy Marathahalli
No 7, J J Complex,
ITPB Road, Aswath Nagar,
Marathahalli Post,
Bengaluru 560037

    :   93450 45466

Coimbatore

FITA Academy - Saravanampatty
First Floor, Promenade Tower,
171/2A, Sathy Road, Saravanampatty,
Coimbatore - 641035
Tamil Nadu

    :   95978 88270

FITA Academy - Singanallur
348/1, Kamaraj Road,
Varadharajapuram, Singanallur,
Coimbatore - 641015
Tamil Nadu

    :   95978 88270

Other Locations

FITA Academy - Madurai
No.2A, Sivanandha salai,
Arapalayam Cross Road,
Ponnagaram Colony,
Madurai - 625016, Tamil Nadu

    :   97900 94102

FITA Academy - Pondicherry
410, Villianur Main Rd,
Sithananda Nagar, Nellitope,
Puducherry - 605005
Near IG Square

    :   93635 21112

FITA Academy - Tiruppur
61D, Poongodi Towers 2nd floor,
Periyar Colony Bus Stop,
Tirupur - 641 652

    :   9940122502

Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!