In this tutorial, we’ll learn what Python string split() method is and how to properly use it. We’ll go through multiple Python code examples to understand how split method works.
Outline
- Introduction: Python String split() Method
- Syntax of split() Method
- Example 1: split() Method applied on a Python String
- Example 2: split() Method with MaxSplit
- Example 3: Python string split() Method Return Value
- Conclusion
Introduction: Python String split() Method
This method breaks a string and return a list of strings. We can specify from which character/substring, the string should break. If not specified any, then whitespaces will be used.
Syntax of split() Method
string.split(separator(optional), maxSplitNumbers(optional))
- Python string split method takes two arguments.
- First argument is used to specify which character should be used to split the string.
- Second argument specify the number of splits that should be performed.
- Python string split method return a list of substrings after breaking/splitting the main string.
Example 1: split() Method applied on a Python String
print('Python Programming Python Language Programming.'.split('Python')) print('Python Programming Python language'.split()) print('Python Programming'.split('pro')) print('Python-Programming-Language'.split('-')) print('Python3Programming3Python3language'.split('3'))
Output
['', ' Programming ', ' Language Programming.'] ['Python', 'Programming', 'Python', 'language'] ['Python Programming'] ['Python', 'Programming', 'Language'] ['Python', 'Programming', 'Python', 'language']
This is how Python string split method works. You can try it with even more examples to better understand the working of this method.
Example 2: split() Method with MaxSplit
print('a,b,c,d,e,f,g,h'.split(',',3)) print('a-b-c-d-e-f-g-h'.split('-',0)) print('a b c d e f g h'.split(' ',4)) // whitespace separator print('a,b,c,d,e,f,g,h'.split(',',1)) print('a,b,c,d,e,f,g,h'.split('',4)) // empty separator gives error
Output
['a', 'b', 'c', 'd,e,f,g,h'] ['a-b-c-d-e-f-g-h'] ['a', 'b', 'c', 'd', 'e f g h'] ['a', 'b,c,d,e,f,g,h'] ValueError: empty separator
So this is how we can limit the number of splits in a string. Splitting starts from left and can be seen in the above examples.
Example 3: Python String split() Method Return Value
val= 'a,bb,c,d,e,f,g,h' returnedVal=val.split(',',4) print(returnedVal)
Output
['a', 'bb', 'c', 'd', 'e,f,g,h']
We can see that Python string split method has returned a list of substrings after splitting the main string. This method uses whitespace as a default separator, if no separator is specified in split method.
Conclusion
To conclude this tutorial, hope you now have a detailed practical understanding of how to properly use Python string split method. I’d be very happy to receive your feedback on this post. Thank you for reading it.
You may like to read:
How To Easily Use Python String RIndex Method [Detailed Python Guide]
How To Easily Use Python String Replace Method [Detailed Python Guide]
How To Easily Use Python String RFind Method [Detailed Python Guide]
How To Use Python String RStrip Method – Easy Python Code Examples
How To Easily Use Python String RPartition Method [Detailed Python Guide]
How To Use Python String IsLower() Method – Easy Python Code Examples
How To Use Python String Upper Method – Easy Python Example Code
How To Use Python String LStrip Method – Easy Python Code Examples
How To Use Python String Isidentifier() Method – Easy Python Code Guide
How To Use Python String Strip Method – Easy Python Guide
How To Properly Use Python String Partition Method [Easy Python Guide]