A string can be split into substrings using the split(param) method. This method is part of the string object. The parameter is optional, but you can split on a specific string or character.

Given a sentence, the string can be split into words. If you have a paragraph, you can split by phrase. If you have a word, you can split it into individual characters.

In most cases, the split() method will do. For characters, you can use the list method.

Related course: Complete Python Programming Course & Exercises

String split

If you have a string, you can subdivide it into several strings. The string needs to have at least one separating character, which may be a space.

By default the split method will use space as separator. Calling the method will return a list of all the substrings.

String to words

The demo below splits a string into characters.

1
2
3
s = "Its to easy"
words = s.split()
print(words)

The len() method will give you the number of characters and number of words:

1
2
print(len(words))
print(len(s))

Output should be similar to the image below:

string split

String to characters

If you want to split a word into characters, use the list() method instead:

1
2
3
word = "Easy"
x = list(word)
print(x)

A string can be reconstructed with the join method, which combines a sequence into a new string.

If you are a beginner, then I highly recommend this YouTube Python Playlist.

Exercises

  1. Can a string be split on multiple characters?
  2. Can you split a string this string?: World,Earth,America,Canada
  3. Given an article, can you split it based on phrases?