How to split string into list in Python

In this post, we will see how to split String into list in python.
There are times when you need to split your String by some delimiter, convert to list and then process it further.
You can use string’s split function to convert String into list in Python.
Let’s understand this with the help of simple example.

#Split by space st='This is sample tutorial' list1=st.split(' ') print(type(list1),list1) #Split by comma emp='John,Male,18' listJ=emp.split(',') print(type(listJ),listJ)


[‘This’, ‘is’, ‘sample’, ‘tutorial’]
[‘John’, ‘Male’, ’18’]

You can map it to invidual variable too.

#Split by comma emp='John,Male,18' name,sex,age=emp.split(',') print("Name:",name) print("Sex:",sex) print("Age:",age)


Name: John
Sex: Male
Age: 18

Leave a Reply

Your email address will not be published. Required fields are marked *