Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to capitalize each word\'s first letter
Let's understand the problem statement: we need to convert a given string to a title case, which involves capitalizing the first letter of each word while converting the remaining letters to lowercase. The following are the various ways to capitalize each word's first letter in a string ?
- Using title() method
- Using capitalize() method
- Using upper() method
- Importing string module
- Importing regex module
- Capitalizing a String in a File
Using the title() Method
The title() method in Python strings is used to capitalize the first letter of each word in a string while converting the remaining letters to lowercase. It ensures that every word starts with an uppercase letter.
Syntax
string.title()
Example
In the following example, we have used the title() method to capitalize each word's first letter ?
my_string = "welcome to tutorialspoint"
print("Original String:", my_string)
new_str = my_string.title()
print("Capitalized String:", new_str)
Original String: welcome to tutorialspoint Capitalized String: Welcome To Tutorialspoint
Using capitalize() Method
The capitalize() method in Python is used to convert the first character of a string to uppercase while ensuring that all other characters in the string are converted to lowercase. This method is useful for standardizing text where only the first letter of the entire string needs to be capitalized.
Syntax
string.capitalize()
Example
Here, we iterate through a string using a for loop to capitalize each word using the capitalize() method. First, we convert the string into a list using the split() method. After capitalizing each word, we use the join() method to convert the list back into a string ?
string_1 = "welcome to tutorialspoint"
print("Original String:", string_1)
words = string_1.split()
capitalized_words = []
for word in words:
capitalized_words.append(word.capitalize())
result = " ".join(capitalized_words)
print("Capitalized String:", result)
Original String: welcome to tutorialspoint Capitalized String: Welcome To Tutorialspoint
Using upper() Method
The upper() method is used to convert all characters in a string to uppercase. This method ensures that every letter in the string is transformed to its uppercase form, leaving non-alphabetic characters unchanged.
Syntax
string.upper()
Example
We can convert the first letter of each word to uppercase using the upper() method. First, we convert the string to a list using the split() method. Then, we capitalize the character at index 0 of each word and append the remaining characters to a new list. Finally, we join the list back into a string using the join() method ?
string_1 = "welcome to python tutorials"
print("Original String:", string_1)
words = string_1.split()
capitalized_words = []
for word in words:
capitalized_words.append(word[0].upper() + word[1:])
result = " ".join(capitalized_words)
print("Capitalized String:", result)
Original String: welcome to python tutorials Capitalized String: Welcome To Python Tutorials
Using the string Module
The string module contains a capwords() method, which capitalizes each word's first character. For this method, we need to import the string module using the import keyword.
Example
In the following example, we have capitalized the first character of each word in a string using the string module ?
import string
my_string = "hello world"
print("Original String:", my_string)
capitalized_string = string.capwords(my_string)
print("Capitalized String:", capitalized_string)
Original String: hello world Capitalized String: Hello World
Using Regular Expressions
In Python, regex (regular expressions) is a special sequence of characters that helps match or find other strings. Using regex, we can search for the starting character of each word and capitalize it.
To use this method, we need to import the re library. This method only capitalizes the first character of each word in a string and does not modify the whitespaces between the words.
Example
In the following example, we have capitalized each word's first character in a string using the re module ?
import re
def convert_to_uppercase(match):
return match.group(1) + match.group(2).upper()
string = "python is an object oriented programming language"
result = re.sub("(^|\s)(\S)", convert_to_uppercase, string)
print("Original String:", string)
print("Capitalized String:", result)
Original String: python is an object oriented programming language Capitalized String: Python Is An Object Oriented Programming Language
Capitalizing a String in a File
We can also capitalize each word in a file. For this, we first create a file using the open() method in 'w' write mode. We then use the open() method to open the file in reading mode and iterate through every line using a for loop.
We can capitalize the first letter of every word using the title() method.
Example
The following is an example of capitalizing a string in a file ?
# Writing to file
with open('capitalize.txt', 'w') as f:
f.write("welcome to tutorialspoint")
# Reading and capitalizing
with open('capitalize.txt', 'r') as f:
for line in f:
output = line.title()
print("Original:", line.strip())
print("Capitalized:", output.strip())
Original: welcome to tutorialspoint Capitalized: Welcome To Tutorialspoint
Comparison
| Method | Best For | Complexity |
|---|---|---|
title() |
Quick title case conversion | Simple |
capitalize() with loop |
Word-by-word control | Medium |
upper() with slicing |
Manual character manipulation | Medium |
string.capwords() |
Better handling of multiple spaces | Simple |
| Regular expressions | Complex pattern matching | Advanced |
Conclusion
The title() method is the simplest approach for capitalizing each word's first letter in Python. For more control over whitespace handling, use string.capwords(). Regular expressions provide the most flexibility for complex capitalization patterns.
