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.
(string-split.py)
# This program demonstrates the split method.
def main():
# 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.
print word-list
# Call the main function.
main()
Output
['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.
Bookmarks