Search code snippets, questions, articles...

Methods to Convert a List to String using Python

Different methods to convert a Python List to String. We will be explaining the code example that will be using the join() function of the Python string.
# define a list
my_list = ['D', 'e', 'v', 's', 'h', 'e', 'e', 't']

# use join() function to convert list to string
result = "".join(my_list)

print(result)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Output

Devsheet
  1. Defined a Python List my_list.
  2. Using "".join(my_list), to concatenate the list items and create a string from them.
  3. Print the result.

Use For loop to convert List to String

If you do not want to use any method to convert a List to String, you can also use Python For loop. We will be applying for loop on the list and in each iteration, we will append the item to String variable result.

my_list = ['D', 'e', 'v', 's', 'h', 'e', 'e', 't']

result = ''

for item in my_list:
  result += item

print(result)

Output

Devsheet
words = ["Hello", "World"]

result = " ".join(words)

print(result)

# -> Hello World
Here we are generating a string from Python List items with space between each item.
Was this helpful?
0 Comments