python string to list code example

Example 1: python string to array

>>> text = 'a b c'
>>> text = text.split(' ')
>>> text
[ 'a', 'b', 'c' ]

Example 2: convert string to list python

# To split the string at every character use the list() function
word = 'abc'
L = list(word)
L
# Output:
# ['a', 'b', 'c']

# To split the string at a specific character use the split() function
word = 'a,b,c'
L = word.split(',')
L
# Output:
# ['a', 'b', 'c']

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: python convert string to list

fruit = 'apple oranges banans'
newlist = fruit.split()
print(newlist)

Example 5: python string to tuple

l = list('abcd')  	        # ['a', 'b', 'c', 'd']
l = map(None, 'abcd')       # ['a', 'b', 'c', 'd']
l = [i for i in 'abcd']	    # ['a', 'b', 'c', 'd']

import re               # importing regular expression module
l = re.findall('.', 'abcd')
print(l)                	# ['a', 'b', 'c', 'd']

Example 6: string to list python

s = 'hello'
l = list(s)
print(l) # prints ['h', 'e', 'l', 'l', 'o']