How to Convert Python List to String with Examples – My programming school

In this instructional exercise, we will figure out how to convert Python List to String. A few models are given to serve a reasonable arrangement.

Python program gives a join() a strategy that takes a succession and converts it’s anything but a string List can contain any of the accompanying article types: Characters, String, Numbers.

While programming you will discover where you need to change a list to a string. We should now perceive how might we utilize the join() strategy in such various cases.

How to Convert Python List to String

As we have told above the Python Join() function that can simply convert a list to a string so first check out its syntax down.

Syntax

The syntax of the join() function is as follows:

string_token.join( iterable )

Parameters:
iterable=>It can be a list of characters,strings, and numbers
string_token=>It is also a string such as a space' ' or comma"," etc.

The above strategy joins every one of the components present in the iterable isolated by the string token. Presently, you will see the join() work guides to convert a list to a string.

Convert Python List of Strings to a string using join() function

Here we have a list of months.

#List of month names
listOfmonths=["Jan" , "Feb", "Mar", "April", "May", "Jun", "Jul"]

Now, we will try to join every of the string in the above list considering space as a separator.

"""
Description: 
Capacity to convert List of strings to a string with a separator
"""
def convert_to_str(input_seq, seperator):
   #Join all the strings in list
   final_str =seperator.join(input_seq)
   return final_str

#List of month names
list_Of_months=["Jan" , "Feb", "Mar", "April", "May", "Jun", "Jul"]

#List of month names separated with a space
seperator= ' '
print("Scenario#1: ",convert_to_str(list_Of_months, seperator))

#List of month names separated with a comma
seperator= ', '
print("Scenario#2: ",convert_to_str(list_Of_months, seperator))

Output:

Scenario#1:  Jan Feb Mar April May Jun Jul
Scenario#2:  Jan, Feb, Mar, April, May, Jun, Jul

Convert a list of chars into a string

With the assistance of the join() technique, we can likewise convert a list of characters to a string. A model is given underneath:

char_List = ['P','y','t','h','o','n',' ','P','r','o','g','r','a','m','m','i','n','g']

#now here we'll convert list of chars into string
final_String= ''.join(charList)

print(char_List)
print(final_String)

Output:

['P', 'y', 't', 'h', 'o', 'n', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']
Python Programming

Conversion of a mixed list to a string using join()

We should expect that we have a list of certain numbers and strings. Since it’s anything but a blend of various items, so we need to deal with it somewhat better.

List1=["I" , "got", 60, "in", "Science", 70, "in", "English",", and", 66, "in", "Maths"]

It’s anything but conceivable to utilize the join() work on this list. We initially need to convert every component to a string to frame another one, and afterward no one but we can call the join() technique.

#Let's convert source_List to a list of strings, and then join its elements.
Final_List= ' '.join([str(item) for item in List1 ])

The final output string would appear something like:

I got 60 in Science,70 in English, and 66 in Maths.

You can check out the complete working source code below:

List1=["I" , "got", 60, "in", "Science,", 70, "in", "English,", "and", 66, "in", "Maths."]

#Let's convert List1 to a list of strings and then join its elements.
Final_List=' '.join([str(item) for item in List1 ])

print(Final_List)

Get a comma-separated string from a list of numbers

On the off chance that you basically wish to convert a comma-isolated string, attempt the underneath alternate way:

num_List=[20, 213, 4587, 7869]
print(str(num_List).strip('[]'))

Output:

20, 213, 4587, 7869

Then again, we can utilize the guide() capacity to convert the things in the list to a string. From that point onward, we can join them as beneath:

print (', '.join(map(str, numList)))

Output:

20, 213, 4587, 7869

We can even use the new line character (‘n’) as a separator. See below:

print ('n'.join(map(str, numList)))

The result is as follows:

20
213
4587
7869

Recommended put up:

my programming school logo
 Source link

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment