Upper case first letter of each word in a phrase

#here is my trial, brief and potent!
str = 'Self contained underwater breathing apparatus'
reduce(lambda x,y: x+y[0].upper(),str.split(),'')
#=> SCUBA

Here's the quickest way to get it done

input = "Self contained underwater breathing apparatus"
output = ""
for i in input.upper().split():
    output += i[0]

This is the pythonic way to do it:

output = "".join(item[0].upper() for item in input.split())
# SCUBA

There you go. Short and easy to understand.

LE: If you have other delimiters than space, you can split by words, like this:

import re
input = "self-contained underwater breathing apparatus"
output = "".join(item[0].upper() for item in re.findall("\w+", input))
# SCUBA

Tags:

Python