Capitalize First Letter of Each Word in Python

0 min read 82 words

If you have a sentence containing multiple words, and you want each of the words to start with a capital letter, then you can do one of the following:

Option 1 - using string.capwords()

import string

string.capwords('this is a test!')

Output: 'This Is A Test!'

Option 2 - using title()

'this is a test!'.title()

Output: 'This Is A Test!'

Option 3 - using join(), split() and list comprehensions

" ".join(w.capitalize() for w in 'this is a test!'.split())

Output: 'This Is A Test!'

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts