xxxxxxxxxx
fruit = 'apple oranges banans'
newlist = fruit.split()
print(newlist)
xxxxxxxxxx
# 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']
xxxxxxxxxx
# Python code to convert string to list
def Convert(string):
li = list(string.split(" "))
return li
# Driver code
str1 = "Geeks for Geeks"
print(Convert(str1))
xxxxxxxxxx
def dpro(string):
convertedlist = list(string.split(" "))
return convertedlist
print(dpro("this will convert to List"))
xxxxxxxxxx
#How to split a string into a list (Python)
#"separator" should be replaced with the string you want to split with
string.split("separator")
xxxxxxxxxx
>>> names='''
Apple
Ball
Cat'''
>>> names
'\n Apple\n Ball\n Cat'
>>> names_list = [y for y in (x.strip() for x in names.splitlines()) if y]
>>> # if x.strip() is used to remove empty lines
>>> names_list
['Apple', 'Ball', 'Cat']
xxxxxxxxxx
#use this self made function when there is no good delimiter for the string
def Convert(string):
list1=[]
#the next line does the changing of the string into an array
#literally it means all elements up to but not including the first one, but just remember it as this is the way we turn a spaceless string into an array
list1[:0]=string
return list1
str1="abcdefghjiklmnopqrstuwxyz"
print(Convert(str1))