Ask coding questions

← Back to all posts
How to split a string using the string module?
madmonster (14)

I am really basic to python and even though I do know which modules for different jobs, I don't know about the string module. Please help me!

Geocube101 (588)

The split() function is built in and doesn't require a module
Syntax: string.split(sep, max_count) where string is the string to modify, sep is the sub-string to split around, and max_count is the amount of times to split the string. max_count is optional and if left blank, will split the string by every occurrence of the sub-string

str1 = 'this is cool'
str1.split(' ')
#=> ['this', 'is', 'cool']
str1.split(' ', 1)
#=> ['this', 'is cool']