Python String capitalize()

Python string capitalize() method will convert the first letter in a string to uppercase and keep the rest of the characters in lowercase. The capitalize() function does not modify the original string and instead returns a string copy.

capitalize() Syntax

The syntax of capitalize() method is:

string.capitalize()

capitalize() Parameter

The capitalize() function does not take any parameters.

Return Value from capitalize()

The capitalize() function returns a copy of the string with the first character capitalized and all other characters lowercased. It doesn’t modify the original string.

Note: In Python 3.8 onwards, the first character is converted into a title case rather than uppercase. It means that characters like digraphs will only have their first letter capitalized instead of the whole character.

Example: Capitalize a string in Python


# Converts the first character to Uppercase/title case
# keeps rest in lowercase
text1 = "python programming"
print(text1.capitalize())

# Converts the first character to Uppercase/title case
# keeps rest in lowercase
text2= "pYTHON Is FUN"
print(text2.capitalize())

# In case of diagraph the first letter is capitalized
text3= "ß"
print(text3.capitalize())


# In case of non alphabets
text4= "*disclaimer"
print(text4.capitalize())

Output

Python programming
Python is fun
Ss
*disclaimer
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Startswith()

Python String startswith()

Table of Contents Hide startswith() Syntaxstartswith() Parameterstartswith() Return ValueExample 1: startswith() Without start and end ParametersExample 2: startswith() With start and end ParametersPassing Tuple to startswith() Python String startswith() method…
View Post
Python Check If String Is Empty Or Not

Check if string is empty or not in Python

Table of Contents Hide Python program to check if string is empty or notUsing len() funtionUsing not operatorUsing not+str.strip() method Using not + str.isspace method In this article, you will learn…
View Post
Menu Driven Program In Python

Menu Driven Program in Python

Introduction to Menu-Driven Program A menu driven program in Python is a program that takes input from a user by displaying a list of options and allows users to choose…
View Post