Python Split() and Join()

When working with strings it is common to need to split the string up at set points such as at spaces. This allows the invidual words of a sentance in a string to be operated on.

Example 1 - Spliting a String into Words

1
2
3
phrase = "the greatest song in the world"
words = phrase.split(" ")
print (words)
This outputs
1
['the', 'greatest', 'song', 'in', 'the', 'world']

Example 2 - Joining the List Back into a Sentance

 1
 2
 3
 4
 5
 6
 7
 8
 9
phrase = "the greatest song in the world"
words = phrase.split(" ")
wordsCaps = []

for word in words:
    word = word[0].upper() + word[1:]
    wordsCaps.append(word)

print (" ".join(wordsCaps))
Returns:

1
2
['The', 'Greatest', 'Song', 'In', 'The', 'World']
The Greatest Song In The World