using function
def make_cap(sentence):return sentence.title()
tryining out
make_cap("hello world")
'Hello World'# it workd but when I have world like "aren't" and 'isn't". how to write function for thata = "I haven't worked hard"
make_cap(a)
"This Isn'T A Right Thing" # it's wrong I am aware of \ for isn\'t but confused how to include it in function
This should work:
def make_cap(sentence):return " ".join(word[0].title() + (word[1:] if len(word) > 1 else "") for word in sentence.split(" "))
It manually splits the word by spaces (and not by any other character), and then capitalizes the first letter of each token. It does this by separating that first letter out, capitalizing it, and then concatenating the rest of the word. I used a ternary if
statement to avoid an IndexError
if the word is only one letter long.