Convert List to String Python have many ways to do. The most common way by using,
' '.join(list_name) where join() is a string method and takes a list of items (Numbers or Strings) to join with the string. In this tutorial, we will look one by one at how to Convert List to String Python.
How to Convert List to String Python Examples
Let’s see the example of and methods you can use to conversion list to string in different ways.
- Using .join() Function
- Using map() and join() Functions
- Traversal of the list to convert into Strings
Using .join() Function
By using this method you can join only strings items of the list, which means the list should only contain strings items. If there is a number then –
TypeError: sequence item 3: expected str instance, int found occurred.
This example using space to join string, you can use comma or hash or else anything.
mylist2 = ['join', 'list', 'string'] # join with space print(' '.join(mylist2))
Output: join list string
Another example using map() and join() Functions
You can use map() to convert each item (Numbers or Strings) in the lists to a string, and then join() them. In this way you can convert the list to sting If it contains just numbers or a mix of strings and numbers or only numbers (int).
This example included how to list to a number code.
mylist1 = [12, 34, 531, 23] # join with space print(' '.join(map(str, mylist1))) mylist2 = ['Eyehunt', 'Python', 'Tutorial'] # join with comma print(','.join(map(str, mylist2)))
Output: 12 34 531 23
Eyehunt, Python, Tutorial
Traversal of the list to convert into Strings
You have “mylist” list, then initialize an empty string “newstr“. Traverse in the list of characters using for loop, for every index add character to the string. After complete traversal, print() the string in the console.
mylist = ['E', 'y', 'e', 'H', 'u', 'n', 't'] newstr = '' for ml in mylist: newstr += ml print(newstr)
However, if you want a single item from the list to strings
You can get any single item from a list using the index. Just pass the value in the list[index] number. If at index position value is an integer then it will integer only you have to convert an integer to strings, by following this link. – https://tutorial.eyehunts.com/python/convert-int-to-string-python-format-examples/
mylist = ['Eyehunt', 'Python', 'Tutorial', 48] print(mylist, type(mylist)) print(mylist, type(mylist))
Output: Eyehunt <class ‘str’>
48 <class ‘int’>
Please write comments anything incorrect or any suggestion always welcome.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Examples of Convert List to String Python are in Python 3, so it may change its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.