π Day 70/150 – Capitalize First Letter of Each Word in Python
Capitalizing the first letter of each word is a common string operation used in titles, names, headings, and text formatting.
✅ Example
Python Programming Languagepython programming language
Output
πΉ Method 1 – Using title()
text = "python programming language"
Python Programming Languageresult = text.title() print(result)
✅ Output
π The title() method automatically capitalizes the first letter of every word.
πΉ Method 2 – Taking User Input
text = input("Enter a string: ") print(text.title())
✅ Example Output
Enter a string: learn python every day
Learn Python Every Day
π Useful when formatting text entered by users.
πΉ Method 3 – Using split() and Loop
Python Programming Languagetext = "python programming language" words = text.split() result = "" for word in words: result += word.capitalize() + " " print(result.strip())
✅ Output
π This method manually capitalizes each word one by one.
πΉ Method 4 – Using List Comprehension
text = "python programming language" result = " ".join([word.capitalize() for word in text.split()]) print(result)
✅ Output
Python Programming Language
π A concise and Pythonic way to capitalize all words.
π₯ Key Takeaways
✅ title() is the easiest method
✅ capitalize() changes the first letter of a word to uppercase
✅ split() separates a sentence into words
✅ join() combines words back into a string
✅ List comprehensions make code shorter and cleaner


0 Comments:
Post a Comment