How to Split a String into a List in Python

Created
Modified

Using split Method

The str.split(sep=None, maxsplit=- 1) method returns a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. For example,

#!/usr/bin/python3

s = "a,b,c,d"

a = s.split(",")
print(a)

b = s.split(",", maxsplit=2)
print(b)
['a', 'b', 'c', 'd']
['a', 'b', 'c,d']

Related Tags

#string# #split# #list#