Split String Into List In Python

Python provides the String type in order to store single or more characters. The string can be split into a list using different methods and ways in Python. Even though there are different ways the split() method is the standard way to split a string into a list.

Split String Into List Using split() Method

The split() method is used to split a string into a list. The split() method requires the delimiter which can be a comma, space, etc. but generally command is used as the default splitter.

sentence = "I,like,pythontect"

words = sentence.split(',')


Split String Into List For Spaces

The split() method can be also used to split string into list by using space as separator. The space is used to separete words in a sentence.

sentence = "I,like,pythontect"

words = sentence.split(' ')


Split String Into List of Integers

A string can be consist of multiple integers those are delimited with different delimeters. In the following example we split the number string named a into the number list named numbers.

a= "1,2,3,4,5,6"

numbers= sentence.split(',')


Leave a Comment