Python Program to Split a String into a List
Beginner · Strings
Splitting a string means cutting it into pieces at a separator, such as a space or a comma, and getting the pieces back as a list. It is how programs read the words of a sentence or the values on a line of a CSV file.
Program
Words: ['Python', 'is', 'easy', 'to', 'learn'] Fruits: ['apple', 'banana', 'cherry', 'mango'] Number of fruits: 4 Second fruit: banana Two commas in a row: ['a', '', 'b']
How it works
split()with no argument cuts the string at every run of spaces, tabs or new lines.split(",")cuts at every comma instead, and the separator itself is not kept in the pieces.- Both return an ordinary list, so
len(), indexing likefruits[1]and loops all work on the result. - With a separator given, two separators in a row leave an empty string between them, as the last line shows.
Other ways to write it
Characters, a limited split and cleaning up
Characters: ['P', 'y', 't', 'h', 'o', 'n'] Split once: ['name', 'Riya=Sharma'] Cleaned: ['red', 'green', 'blue']
list() splits a string into single characters, the second argument of split() limits the number of cuts, and strip() removes the spaces left around each piece.
Without split()
List: ['apple', 'banana', 'cherry', 'mango']
The loop collects characters into current and saves it whenever a comma arrives. The last item has to be saved after the loop, because no comma follows it.
Try it yourself
Split the date "18-10-2026" on "-" and print the day, the month and the year on separate lines.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises