In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do 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 the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
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']
Example 3:
# Python code to convert string to list character-wise def Convert(string): list1=[] list1[:0]=string return list1 # Driver code str1="ABCD"print(Convert(str1)) |
Output:
['A','B','C','D']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Convert List of String List to String List
- Python | Convert list of string to list of list
- Python Program to convert List of Integer to List of String
- Python | Convert list of tuples to list of list
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python | Convert a string representation of list into list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string enclosed list to list
- Python | Convert mixed data types tuple list to string list
- Python | Convert string List to Nested Character List
- Python - Convert String List to Key-Value List dictionary
- Python program to convert a list to string
- Python | Convert a nested list into a flat list
- Python | Convert list of strings and characters to list of characters
- Python | Convert list of tuples into list
- Python | Convert list of tuples to list of strings
- Python | Convert given list into nested list
- Python | Convert list into list of lists
- Python | Convert list of strings to list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sagarbaisoya1

