How to convert Array to String in Python? (convert list to string)


Converting an array to a string or converting a list to a string is a common task that you might need to do in your programming. In this article, we’ll show you how to convert an array to a string in python. We’ll also show you how to convert a string back into an array.

There are a few different ways that you can convert an array to a string. The most common way is by using the built-in function, str(). This function takes in an array and returns a string representation of that array.

You can also use the join() method of the str class. This method takes in an array and returns a string that is the concatenation of all the elements in the array.

Example 1:

my_arr = [1, 2, 3, 4, 5] my_string = ','.join(str(x) for x in arr) print(my_string)
Code language: Python (python)

Output

"1, 2, 3, 4, 5"
Code language: JSON / JSON with Comments (json)

Example 2:

my_sentence = ['This','is','an','apple'] my_string = " ".join(my_sentence) print(my_string)
Code language: Python (python)

Output:

"This is an apple"
Code language: JSON / JSON with Comments (json)

Example 3: We can also use reduce() function in Python:

from functools import reduce sentence_array = ['This','is','an','orange'] output_string = str(reduce(lambda x,y: x + "-" + y, sentence_array)) print(output_string)
Code language: Python (python)

Output:

This-is-an-orange
Code language: Python (python)

Example 4:

list_numbers = ['111', '333', '555'] my_string = ''.join(list_numbers) print(my_string) # output >>> 111333555 my_string = ','.join(list_numbers) print(my_string) # output >>> 111,333,555 my_string = '-'.join(list_numbers) print(my_string) # output >>> 111-333-555 my_string = '\n'.join(list_numbers) print(my_string) # output >>> 111 # output >>> 333 # output >>> 555
Code language: PHP (php)

Example 5: Without .join() method you can use this script:

my_list=["this","is","an","apple"] output_string="" for str in range(len(my_list)): if str == len(my_list)-1: output_string+=my_list[str] else: output_string+=f'{my_list[str]}-' print(output_string) #Output >>> 'this-is-an-apple'
Code language: Python (python)

If you want to convert a string back into an array, you can use the split() method of the str class. This method splits a string up into multiple strings based on a delimiter that you specify.

my_string = "1, 2, 3, 4, 5" y = my_string.split(", ") print(y)
Code language: Python (python)

Output

['1', '2', '3', '4', '5']
Code language: JSON / JSON with Comments (json)

Conclusion:

Converting an array to a string is a common task that you might need to do in your programming. In this article, we’ve shown you how to convert an array to a string in python using both the built-in str() function and the join() method of the str class.

We’ve also shown you how to convert a string back into an array using the split() method of the str class.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts