In Python, you can split a string using the split
method or the re.split
function for regular expression-based splitting.
In this example,
str
with a value of "Hello World"
.split
method with a space as the delimiter.str = 'Hello World'
parts = str.split()
print(parts)
['Hello', 'World']
In this example,
str
with a value of "apple,banana,cherry"
.split
method with a comma as the delimiter.str = 'apple,banana,cherry'
parts = str.split(',')
print(parts)
['apple', 'banana', 'cherry']
In this tutorial, we learned How to Split a String in Python language with well detailed examples.