How to split string in Python

The built-in function split() can be used to split string in Python. This method will return the list of strings after splitting the string by the specified separator.
Syntax: string.split(separator[, maxlimit])
- separator – an optional delimiter. The string splits at the specified separator. The default value of split if any whitespace.
- maxlimit – optional. Defines the maximum number of splits. The default value is -1, which is no limit to splits.
Now let’s take a look at some examples and understand how this works.
#Example:
str = 'Hello World! Pythonpoint is awesome' #splits at space.default case print(str.split()) #splits at '!' print(str.split('!')) #maxlimit is 4 print(str.split(' ', 4)) #maxlimit is 3 print(str.split(' ', 3))
Output:
['Hello', 'World!', 'Pythonpoint', 'is', 'awesome'] ['Hello World', ' Pythonpoint is awesome'] ['Hello', 'World!', 'Pythonpoint', 'is', 'awesome'] ['Hello', 'World!', 'Pythonpoint', 'is awesome']
Subscribe
Login
Please login to comment
0 Discussion