What is split used for?
This method separate a string into a list. You can specify the separator, when this isn’t defined, whitespace(” “)
is used.
Example
- Separator not defined:
Text = "Today is very nice day"
txt = Text.split()
print(txt)
# Output:
['Today', 'is', 'very', 'nice', 'day']
Example
- Split the string, using comma:
Text = "Today, is very nice, day"
txt = Text.split(",")
print(txt)
# Output:
['Today', ' is very nice', ' day']
Example
- Use a hash character as a separator:
Text = "Today#is#very#nice#day"
txt = Text.split("#")
print(txt)
# output:
['Today', 'is', 'very', 'nice', 'day']
Example
- Split the string into a list with max 2 items:
Text = "Today#is#very#nice#day"
txt = Text.split("#", 2)
print(txt)
# Output:
['Today', 'is', 'very#nice#day']