Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize some words in a text file?

I have a text file which have normal sentences. Actually I was in hurry while typing that file so I just capitalized the first letter of first word of the sentence (as per English grammar).

But now I want that it would be better if each word's first letter is capitalized. Something like:

Each Word of This Sentence is Capitalized

Point to be noted in above sentence is of and is are not capitalized, actually I want to escape the words which has equal to or less than 3 letters.

What should I do?

like image 608
Santosh Kumar Avatar asked Dec 12 '22 23:12

Santosh Kumar


1 Answers

for line in text_file:
    print ' '.join(word.title() if len(word) > 3 else word for word in line.split())

Edit: To omit counting punctuation replace len with the following function:

def letterlen(s):
    return sum(c.isalpha() for c in s)
like image 101
Steven Rumbalski Avatar answered Dec 25 '22 22:12

Steven Rumbalski