Open In App

String capitalize() Method in Python

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Python3




# initializing a string
name = "draco malfoy"
#using capitalize function
print(name.capitalize())


Output

Draco malfoy

Definition of capitalize() Function

The capitalize() function in Python is used to capitalize the first letter of a string. It turns the rest of all letters to lowercase.

If the first letter is not an alphabet, capitalize() function will not change anything in the string.

capitalize() Method Syntax

string_name.capitalize()

Parameter

The capitalize() function does not take any parameter. 

Return

The capitalize() function returns a string with the first character in the capital.

capitalize() Method Examples

Let’s look at some capitalize in Python examples, to understand the function better.

Python




name = "geeks FOR geeks"
 
print(name.capitalize())


Output:

Geeks for geeks

Example 1: capitalize() only capitalizes the first letter of a string and lowers all the remaining characters

Python3




string = "roses are red"
print("Original string:", string)
print("After using capitalzie:", string.capitalize())


Output:

Original string: roses are red
After using capitalzie: Roses are red

Example 2: Python String capitalize() Method Doesn’t Modify the Original String

Python String capitalize() Method creates and returns a copy of the original string after modifications.

Python3




string = "geeks for geeks"
string_2 = string.capitalize()
 
print("New string after using capitalize():", string_2)
 
print("Original string:", string)


Output

New string after using capitalize(): Geeks for geeks
Original string: geeks for geeks

We have covered how to capitalize a string in Python using the capitalize() function. It is useful for data formatting.

Related Article: String Methods



Previous Article
Next Article

Similar Reads

How to capitalize first character of string in Python
The problem of case changes in a string is quite common and has been discussed many times. Sometimes, we might have a problem like this in which we need to convert the initial character of the string to the upper case. Let’s discuss certain ways in which this can be performed. Method #1: Using string slicing + upper() This task can easily be perfor
5 min read
Python - Capitalize repeated characters in a string
Given an input string with lowercase letters, the task is to write a python program to identify the repeated characters in the string and capitalize them. Examples: Input: programming languageOutput: pRoGRAMMiNG lANGuAGeExplanation: r,m,n,a,g are repeated elements Input: geeks for geeksOutput: GEEKS for GEEKSExplanation: g,e,k,s are repeated elemen
4 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
Python Capitalize First Letter of Every Word in String
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 c
3 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
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
Python program to capitalize the first letter of every word in the file
The following article contains programs to read a file and capitalize the first letter of every word in the file and print it as output. To capitalize the first letter we will use different methods using Python. The Python String Method is used to convert the first character in each word to Uppercase and the remaining characters to Lowercase in the
3 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
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
Article Tags :
Practice Tags :
three90RightbarBannerImg