Python | Program for converting a string to a list



In this program we will try to convert the given string to a list containing spaces or any other special characters, depending on the user`s choice. For this we use the split () method. 
Syntax :

string.split("delimiter")

Examples:

 Input: "Geeks for Geeks" Output: [`Geeks`,` for`, `Geeks`] Input:" Geeks-for-Geeks "Output: [` Geeks`, `for`,` Geeks`]  

The split method is used to split strings and store them in a list. The built-in method returns a list of words in a string using the "delimiter" as the delimiter string. If no delimiter is specified or is missing, a different splitting algorithm is applied: a series of consecutive spaces are treated as a single delimiter, and the result is no leading or trailing blank lines if the string has a leading or trailing space. 
Example 1:

# 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))

Output:

 [`Geeks`,` for`, `Geeks`] 

Example 2:

# 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))

Output:

 [`Geeks`,` for`, `Geeks`]