Do you want to split a string into a List of Words or Do you want to Split a String into a list of Letters? This article will show you how you can convert the string to a list of either words or letters in Python.
In Python, we have a method or function named split in String class that can be used to split or convert the list of words for a given String in Python. Below is the example of usage of the split method of string class.
How to Split a String into a List of Words in Python
To convert or split a string into a list of words you can directly use the split() function on the given string. If no delimiter or default delimiter is used inside the split method then it will return the list of Words.
Let us see that in the below example code for the usage of Split Method on given String.
#Initializing the String to Split or #Convert it to List of Words givenString = "You are Visiting Coduber Website." #Using Split Method with Default Delimiter #To Split the method based on space between #the Words listOfWord = givenString.split() #Priting the List of Words print(listOfWord)
['You', 'are', 'Visiting', 'Coduber', 'Website.']
As shown in the above code, using the split method with nothing inside the small braces splits the words based on the spaces present in the given string.
Split the String Using Delimiter in Python
If you are having a string that is not based on space but for example is based on the hyphen. Let’s take an example that you have a string that is URL and you want to split it into Words. To do that you need to use the hyphen delimiter inside the braces of the split.
Let us see in the below example code the usage of the split() method with delimiter in Python.
#Initializing the String to Split or #Convert it to List of Words givenString = "split-string-with-single-delimiters-in-python" #Using Split Method with Single Delimiter #To Split the method based on hyphen between #the Words listOfWord = givenString.split('-') #Priting the List of Words print(listOfWord)
['split', 'string', 'with', 'single', 'delimiters', 'in', 'python']
As shown above, using a single delimiter in the split method I was able to split the string into words based on the hyphen.
Split the String into List Using Multiple Delimiter
Now to extend the above problem statement if you have a string that you want to split or extract the words into a list using multiple delimiters then you cannot use the split method of String Class or Library.
In this you need to use the split method present in regex Library, to split the given string based on the multiple delimiters. Let us see in the below example code the usage of regex to split the string based on a delimiter.
#importing regex Library import re #Initializing the String to Split or #Convert it to List of Words using #multiple delimiters givenString = "https:coduber.com/reading-a-binary-file-in-python-with-examples" #Using regex Split Method with Single Delimiter #To Split the method based on hyphen between #the Words listOfWord = re.split('[:./-]', givenString) #Priting the List of Words print(listOfWord)
['https', 'coduber', 'com', 'reading', 'a', 'binary', 'file', 'in', 'python', 'with', 'examples']
As shown in the above code using the regex split function I was able to split the string into a list of words based on the multiple delimiters of colon, dash, slash, and dot. I have mentioned all the delimiters inside the square bracket as it makes the code look cleaner.
Note: If you will have delimiters present in the string back to back then you may encounter an empty string in the list. You need to take care of that if you do not want the empty string in the list.
How To Split a String into List of Letters in Python
Now another question in this problem is how to convert or split the string into a list of letters. Here you do not want it to be in Words rather you want it to be in Letters.
This problem is fairly simple to solve and you are not required to use the split method here. All you need to do is use the list constructor.
Let us see the usage of List Constructor to convert String into List of Letters.
#Convert it to List of Letters using #Using List Constructor givenString = "Convert to List of Letters" #Using list Constructor listOfWord = list(givenString) #Priting the List of Words print(listOfWord)
['C', 'o', 'n', 'v', 'e', 'r', 't', ' ', 't', 'o', ' ', 'L', 'i', 's', 't', ' ', 'o', 'f', ' ', 'L', 'e', 't', 't', 'e', 'r', 's']
As shown in the above code using List Constructor I easily converted the given String into a List of Letters.
Or alternatively, you can use the split method to split the string into a list of Words, and then you can convert those words into a list of letters using List Constructor. But that will be really a complicated method to follow.
Hope You were able to learn about splitting and converting a string into words and letters with and without delimiters in Python. You can solve your problem with the split method of either string class or regex library as per your requirement.
Let me know in the comment section if you have any better solution than the above-mentioned. I will be happy to add it here.