How can I convert a list to a string using Python?
Aran-Fey
36.6k10 gold badges96 silver badges140 bronze badges
asked Apr 11, 2011 at 8:51
3
Use ''.join
:
xs = ['1', '2', '3']
s = ''.join[xs]
If the list contains integers, convert the elements to string before joining them:
xs = [1, 2, 3]
s = ''.join[str[x] for x in xs]
Mateen Ulhaq
22.2k16 gold badges86 silver badges127 bronze badges
answered Apr 11, 2011 at 8:52
Senthil KumaranSenthil Kumaran
52.3k14 gold badges89 silver badges127 bronze badges
6
>>> xs = [1, 2, 3]
>>> " ".join[str[x] for x in xs]
'1 2 3'
Mateen Ulhaq
22.2k16 gold badges86 silver badges127 bronze badges
answered Apr 11, 2011 at 8:53
Andrey SboevAndrey Sboev
7,3241 gold badge19 silver badges35 bronze badges
1
xs = ['L', 'O', 'L']
lol_string = ''.join[map[str, xs]]
Mateen Ulhaq
22.2k16 gold badges86 silver badges127 bronze badges
answered Apr 11, 2011 at 8:56
NathanNathan
5,97510 gold badges44 silver badges54 bronze badges
3
❮ String Methods
Example
Split a string into a list where each word is a list item:
txt = "welcome to the jungle"
x = txt.split[]
print[x]
Try it Yourself »
Definition and Usage
The split[]
method splits
a string into a list.
You can specify the separator, default separator is any whitespace.
Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Syntax
string.split[separator, maxsplit]
Parameter Values
separator | Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator |
maxsplit | Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences" |
More Examples
Example
Split the string, using comma, followed by a space, as a separator:
txt = "hello, my name is Peter, I am 26 years old"
x = txt.split[", "]
print[x]
Try it Yourself »
Example
Use a hash character as a separator:
txt = "apple#banana#cherry#orange"
x = txt.split["#"]
print[x]
Try it Yourself »
Example
Split the string into a list with max 2 items:
txt = "apple#banana#cherry#orange"
# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split["#", 1]
print[x]
Try it Yourself »
❮ String Methods
Given a list, write a Python program to convert the given list to string. There are various situations we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.
Example:
Input: ['Geeks', 'for', 'Geeks'] Output: Geeks for Geeks Input: ['I', 'want', 4, 'apples', 'and', 18, 'bananas'] Output: I want 4 apples and 18 bananas
Let’s see various ways we can convert the list to string.
Method #1: Iterate through the list and keep adding the element for every index in some empty string.
Python3
def
listToString[s]:
str1
=
""
for
ele
in
s:
str1
+
=
ele
return
str1
s
=
[
'Geeks'
,
'for'
,
'Geeks'
]
print
[listToString[s]]
Method #2: Using .join[] method
Python3
def
listToString[s]:
str1
=
" "
return
[str1.join[s]]
s
=
[
'Geeks'
,
'for'
,
'Geeks'
]
print
[listToString[s]]
But what if the list contains both string and integer as its element. In those cases, above code won’t work. We need to convert it to string while adding to string.
Method #3: Using list comprehension
Python3
s
=
[
'I'
,
'want'
,
4
,
'apples'
,
'and'
,
18
,
'bananas'
]
listToStr
=
' '
.join[[
str
[elem]
for
elem
in
s]]
print
[listToStr]
Output:
I want 4 apples and 18 bananas
Method #4: Using map[] Use map[] method for mapping str [for converting elements in list to string] with given iterator, the list.
Python3
s
=
[
'I'
,
'want'
,
4
,
'apples'
,
'and'
,
18
,
'bananas'
]
listToStr
=
' '
.join[
map
[
str
, s]]
print
[listToStr]
Output:
I want 4 apples and 18 bananas
Method #5: Using enumerate function
Python3
s
=
[
'I'
,
'want'
,
4
,
'apples'
,
'and'
,
18
,
'bananas'
]
listToStr
=
' '
.join[[
str
[elem]
for
i,elem
in
enumerate
[s]]]
print
[listToStr]
Output
I want 4 apples and 18 bananas
Method #6: Using in operator
Python3
s
=
[
'Geeks'
,
'for'
,
'Geeks'
]
for
i
in
s:
print
[i,end
=
" "
]