
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to capitalize each word\\\'s first letter
Let's understand the problem statement: we need to convert a given strings 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 to capitalize
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. Following is the syntax of the Python title() method in the string -
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(new_str)
Following is the output of the above code -
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. Following is the syntax of the Python capitalize() method -
string.capitalize()
Example
Here, we have to 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) list_1 = string_1.split() list_2=[] for i in list_1: list_2.append(i.capitalize()) string =" ".join(list_2) print(string)
Following is the output of the above code -
Original String : welcome to tutorialspoint 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. Following is the syntax of the Python upper() method in a string -
string.upper()
Example
Convert the first letter of each word to uppercase using the upper() method. First, we need to 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) list_1 = string_1.split() list_2=[] for i in list_1: list_2.append(i[0].upper()+i[1:]) string =" ".join(list_2) print(string)
Following is the output of the above code -
Original String : welcome to python tutorials Welcome To Python Tutorials
Importing 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 Sring :",my_String) Cap_String = string.capwords(my_String) print(Cap_String)
Following is the output of the above code -
Hello World
Importing 'regex' Module
In Python, Regex is generally known as a regular expression, a special sequence of characters that helps match or find other strings. Using regex, we can search the starting character of each word and capitalize it.
To use this method, we need to import the regex library using the import keyword. 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-importing regex module -
import re def convert_into_uppercase(a): return a.group(1) + a.group(2).upper() string = "python is an object oriented programming language" result = re.sub("(^|\s)(\S)", convert_into_uppercase, string) print(result)
Following is the output of the above code -
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 need to first create a file using the open() method in a 'w' write mode. We have to use the open() method to open the file in the reading mode and then iterate through every word using for() loop.
We can capitalize the first letter of every word using the title() or capitalize() method.
Example
The following is an example of capitalizing a string in a file -
f = open('capitalize.txt','w') f.write("welcome to tutorialspoint") f = open('capitalize.txt', 'r') for line in f: output = line.title() print(output)
Following is the output of the above code -
Welcome To Tutorialspoint