The Wayback Machine - https://web.archive.org/web/20240811122133/https://www.geeksforgeeks.org/python-capitalize-first-letter-of-every-word-in-string/
Open In App

Python Capitalize First Letter of Every Word in String

Last Updated : 01 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In text manipulation in Python, capitalizing the first letter of every word in a string is a common and essential task. This operation enhances the visual presentation of text and is particularly useful when dealing with user input, titles, or any scenario where proper capitalization is desired. In this article, we will explore some approaches to capitalize the initial letter of each word within a string, offering developers a range of options to suit their specific needs.

Capitalize First Letter of Each Word in String using Python

Below, are the ways of Python to Capitalize the First Letter Of Every Word In String.

Capitalize First Letter Of Every Word using capitalize() Function

In this example, the below code initializes a string, “capitalize the first letter,” and then uses the `capitalize()` method to convert the first letter of the entire string to uppercase. The print statements display both the original and capitalized strings.

Python3




original_string = "capitalize first letter"
capitalized_string = original_string.capitalize()
 
print("Original String:", original_string)
print("Capitalized String:", capitalized_string)


Output

Original String: capitalize first letter
Capitalized String: Capitalize first letter


Capitalize First Letter Of Every Word Using title() Function

In this example , below code sets an original string, “capitalize first letter,” and utilizes the `title()` method to capitalize the first letter of each word. The print statements output both the original and capitalized strings.

Python3




original_string = "capitalize first letter"
capitalized_string = original_string.title()
 
print("Original String:", original_string)
print("Capitalized String:", capitalized_string)


Output

Original String: capitalize first letter
Capitalized String: Capitalize First Letter


Capitalize First Letter Of Every Word Using capwords() Function

In this example, below code imports the `capwords` function from the `string` module and applies it to capitalize the first letter of each word in the original string, “capitalize first letter.” The print statements then display both the original and capitalized strings.

Python3




from string import capwords
 
original_string = "capitalize first letter"
capitalized_string = capwords(original_string)
 
print("Original String:", original_string)
print("Capitalized String:", capitalized_string)


Output

Original String: capitalize first letter
Capitalized String: Capitalize First Letter


Capitalize First Letter Of Every Word Using join() Method

In this example , below code initializes a string, “capitalize first letter,” and uses list comprehension to capitalize the first letter of each word. It then joins these capitalized words into a string. The print statements show both the original and capitalized strings.

Python3




original_string = "capitalize first letter"
capitalized_string = ' '.join([word.capitalize() for word in original_string.split()])
 
print("Original String:", original_string)
print("Capitalized String:", capitalized_string)


Output

Original String: capitalize first letter
Capitalized String: Capitalize First Letter




Similar Reads

JavaScript Program to Capitalize the First Letter of Every Sentence in a String
In this article, we will see how Capitalizing the first letter of every sentence in a string is a common text manipulation task in JavaScript. It involves identifying sentence boundaries and ensuring that the first letter of each sentence is in uppercase. Examples: Input: "this is Geeks for Geeks website. hello there!"Output: "This is Geeks for Gee
2 min read
Capitalize first letter of a column in Pandas dataframe
Analyzing real-world data is somewhat difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important. One might encounter a situation where we need to capitalize any specific column in given dataframe. Let's see how can we capitalize
2 min read
Python program to capitalize the first and last character of each word in a string
Given the string, the task is to capitalize the first and last character of each word in a string. Examples: Input: hello world Output: HellO WorlDInput: welcome to geeksforgeeksOutput: WelcomE TO GeeksforgeekSApproach:1 Access the last element using indexing.Capitalize the first word using the title() method.Then join each word using join() method
2 min read
String capitalize() Method in Python
Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter while making all other characters in the string lowercase letters. Example C/C++ Code # initializing a string name = "draco malfoy" #using capitalize function print(name.capitalize()) Outpu
2 min read
Capitalize Each String in a List of Strings in Python
In Python, manipulating strings is a common task, and capitalizing each string in a list is a straightforward yet essential operation. This article explores some simple and commonly used methods to achieve this goal. Each method has its advantages and use cases, providing flexibility for different scenarios. Capitalize Each String In A List Of Stri
3 min read
Word and Letter Counter using React
The word and letter counter is an application that counts the number of letters and words entered in text and give the number (count) of each in output. Each element present in the text will be considered a letter, and a group of letters with a space will be considered a word. Using this approach, we implement the counter. Preview of Final Output:
3 min read
numpy.defchararray.capitalize() in Python
numpy.core.defchararray.multiply(arr, n): Capitalizes the first letter of string element-wise. Parameters: arr : array-like or string. Returns : Capitalized first letter of the string. Code #1: # Python Program illustrating # numpy.char.capitalize() method import numpy as np arr1 = ['eAAAaeAAAa', 'ttttdsttttds', 'AAtAAt'] arr2 = ['11sf', 'sdsf2', '
1 min read
Python | Pandas Series.str.capitalize()
Series.str.capitalize() used to capitalize the string elements in pandas series. Series is a type of data structure which is used as same as list in python. Series can contain the different types of data as we want to enter in the series like list. Syntax : Series.str.capitalize() Parameters: None Returns: Series/Index of objects To get the link to
2 min read
PyQt5 QSpinBox - Making text capitalize
In this article we will see how we can make the text of the spin box capitalize, in order to set the font use setFont method which takes QFont object as argument. In order to make the text i.e font capitalize, we have to get the QFont object of the spin box then make it capitalize then reassign it to the spin box. In order to do this we use setCapi
2 min read
PyQt5 QSpinBox – Checking if text is capitalize ?
In this article we will see how we can if the text of the spin box is capitalize or not, in order to set the font we use setFont method which takes QFont object as argument. In order to make the text i.e font capitalize, we have to get the QFont object of the spin box then make it capitalize then reassign it to the spin box. In order to do this we
2 min read
three90RightbarBannerImg