This is a discussion on Splitting a String in Python within the Programming forums, part of the Tutorials category; Splitting a String in Python:- Strings in Python have a method named split that returns a list of the words ...
Splitting a String in Python:-
Strings in Python have a method named split that returns a list of the words in the string. Program (given below) shows an example.
# This program demonstrates the split method.
# Create a string with multiple words.
my-string = 'One two three four'
# Split the string.
word-list = my-string.split()
# Print the list of words.
# Call the main function.
['One', 'two', 'three', 'four']
By default, the split method uses spaces as separators (that is, it returns a list of the words in the string that are separated by spaces). You can specify a different separator by passing it as an argument to the split method.