Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upper case first letter of each word in a phrase

Tags:

python

Ok, I'm trying to figure out how to make a inputed phrase such as this in python ....

Self contained underwater breathing apparatus

output this...

SCUBA

Which would be the first letter of each word. Is this something to do with index? and maybe a .upper function?

like image 621
Pr0cl1v1ty Avatar asked Apr 25 '11 06:04

Pr0cl1v1ty


People also ask

What's it called when you capitalize the first letter of each word?

Capitalization (American English) or capitalisation (British English) is writing a word with its first letter as a capital letter (uppercase letter) and the remaining letters in lower case, in writing systems with a case distinction.

What is capitalize each word?

To capitalize a word is to make its first letter a capital letter—an uppercase letter. For example, to capitalize the word polish (which is here spelled with a lowercase p), you would write it as Polish. A word whose first letter is a capital can be described as capitalized.


1 Answers

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
like image 101
Gabi Purcaru Avatar answered Oct 16 '22 12:10

Gabi Purcaru