A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/string-capitalize-python/ below:

String capitalize() Method in Python

String capitalize() Method in Python

Last Updated : 09 May, 2025

The capitalize() method in Python is used to change the first letter of a string to uppercase and make all other letters lowercase. It is especially useful when we want to ensure that text follows title-like capitalization, where only the first letter is capitalized.

Let's start with a simple example to understand the capitalize() functions:

Python
s = "hello WORLD"
res = s.capitalize()
print(res)

Explanation: In this example, capitalize() method converts the first character "h" to uppercase and change the rest of characters to lowercase.

Syntax of capitalize() Method

s.capitalize()

Parameters:

Return Value: A new string with the first character capitalized and all others in lowercase.

Note: The original string remains unchanged (strings are immutable in Python).

Examples of capitalize() Method Example 1: Multiple Words

The capitalize() method only affects the first letter of the string and doesn’t capitalize each word in multi-word strings.

Python
s = "multiple WORDS IN a String"
res = s.capitalize()
print(res)

Output
Multiple words in a string

Explanation:

Example 2: When the First Character is a Number

When the first character of a string is a number, capitalize() method does not alter it because it only affects alphabetical characters. Instead, it will simply convert any following alphabetical characters to lowercase.

Python
s = "123hello WORLD"
res = s.capitalize()
print(res)

Explanation:

Related Article:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4