Python List to String

Previous

In this post, we will see how to convert a list to String in Python.

You can simply use string’s join method to convert a list to String in Python.


String’s join method

string’s join method return string in which element of the sequence is joined by separator.

Let’s see some examples.


Join all the strings in list without any separator

listOfChars = ["a" , "b", "c", "d", "e"] ''.join(listOfChars)

Output:

‘abcde’

Join all strings in list with space

listOfWords=["Hello", "world", "from", "java2blog"] ' '.join(listOfWords)

Output:

‘Hello world from java2blog’

Join all the strings in list with comma

listOfCountries=["India", "China", "Nepal", "France"] countries_str=','.join(listOfCountries) print(countries_str)

Output:

HIndia,China,Nepal,France

Convert list of ints to string in python

If you have list of ints, you can convert each item to string using map function.

list1 = [1, 2, 3, 4] str1 = ''.join(map(str, list1)) print(str1)

Output:

1234

That’s all about Python List to String.

Previous

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog