convert string to list python

28

# 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))
def dpro(string):
    convertedlist = list(string.split(" "))
    return convertedlist
  
print(dpro("this will convert to List"))
>>> 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']

Comments

Submit
0 Comments