python turn list into string code example

Example 1: convert list to string python

# Python program to convert a list 
# to string using list comprehension 
   
s = ['I', 'want', 4, 'apples', 'and', 18, 'bananas'] 
  
# using list comprehension 
listToStr = ' '.join([str(elem) for elem in s]) 
  
print(listToStr)

Example 2: how to convert list into string in python

list_of_num = [1, 2, 3, 4, 5]
# Covert list of integers to a string
full_str = ' '.join([str(elem) for elem in list_of_num])
print(full_str)

Example 3: how do i convert a list to a string in python

lines2 = " ".join(lines) 	# Converts the list to a string.
							# This should work for writing to a file
file.write(lines2)

Example 4: how to convert a list into string with \n

int_lst = [1, 2, 3, 4, 5]
full_str = '\n'.join([str(elem) for elem in list_of_num])
print(full_str)

Example 5: python array to string

mystring = 'hello, world'

myarray = string1.split(', ') #output => [hello, world]

myjoin1 = ', '.join(myarray) #output => hello, world
myjoin2 = ' '.join(myarray) #output => hello world
myjoin3 = ','.join(myarray) #output => hello,world

Example 6: python list to string

mystring = 'hello, world'

mylist = string1.split(', ') #output => ['hello', 'world']

myjoin1 = ', '.join(mylist) #output => 'hello, world'
myjoin2 = ' '.join(mylist) #output => 'hello world'
myjoin3 = ','.join(mylist) #output => 'hello,world'

Tags:

Java Example