Open In App

How to Capitalize First Character of String in Python

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Suppose we are given a string and we need to capitalize the first character of it, for example:

Input: “geeks”
Output: “Geeks”

In this article, we are going to learn several ways of doing it with examples, let’s look at them:

Using str.capitalize()

Using str.capitalize() is a simple way to make the first character of a string uppercase while converting the rest of the characters to lowercase.

Python
s = "hello world"

res = s.capitalize()

print(res)

Output
Hello world

Explanation: s.capitalize() method transforms the first character of the string s to uppercase and converts the remaining characters to lowercase.

Using Slicing

Using slicing to capitalize the first letter of a string involves manipulating the string by selecting specific portions and applying transformations.

Python
s = "geeksforgeeks"

res = s[0].upper() + s[1:]

print(res)

Output
Geeksforgeeks

Explanation:

  • s[0] extracts the first character of the string, and s[1:] extracts the rest of the string starting from the second character.
  • upper() method is applied to the first character to capitalize it, and concatenation (+) combines it with the remaining part of the string, resulting in “Geeksforgeeks“.

Using str.title() for Title Case

Using str.title is a convenient method to convert a string to title case, where the first letter of each word is capitalized. This is especially useful for formatting strings like names or titles consistently.

Python
s = "geeks for geeks"

res = s.title()

print(res)

Output
Geeks For Geeks

Explanation: s.title() method capitalizes the first letter of each word in the string s while converting the remaining characters of each word to lowercase.

Using f-strings

Using f-strings in Python provides a concise and efficient way to embed variables or expressions into a string. It is particularly useful for formatting and creating dynamic strings.

Python
s = "hello world"

res = f"{s[0].upper()}{s[1:]}"

print(res)

Output
Hello world

Explanation: f”{s[0].upper()}{s[1:]}” capitalizes the first character of the string s by using slicing (s[0].upper()) and appending the rest of the string unchanged (s[1:]).



Next Article
Practice Tags :

Similar Reads