DEV Community

Cover image for Capitalize()- Manually -NLP
datatoinfinity
datatoinfinity

Posted on • Edited on

Capitalize()- Manually -NLP

Capitalize() is a built in function that is used to capitalize first letter and make all other character lowercased.

text="hey how are you? how it is going?"
print(text.capitalize())
Output:
Hey how are you? how it is going?

Now you see only "H" got capitalize every other character is lowercased.

Now we will write capitalize() function logic manually. Sometimes we need more customised way of writing this function where these function doesn't.

text="hey how are you? how it is going?"
first_char=text[0].upper()
last_char=text[1:].lower()
print(first_char+last_char)
Hey how are you? how it is going?
  • We have used upper() to capitalise first letter
  • We have used lower() to convert all other character lowercased

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.