Python - String String split

Introduction

Consider the following code:

Demo

line = 'aaa bbb  ccc' 
cols = line.split() 
print( cols )

Result

The string split method chops up a string into a list of substrings by a delimiter string.

Here, we didn't pass a delimiter in the prior example, so it defaults to whitespace.

The following code splits the string at commas:

Demo

line = 'AAA,CCC,40' 
print( line.split(',') )

Result

Delimiters can be longer than a single character, too:

Demo

line = "i'mTESTaTESTlumberjack" 
print( line.split("TEST") )

Result