How to Capitalize First Character of String in Python
Last Updated :
11 Apr, 2025
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)
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)
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)
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)
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:]).
Similar Reads
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
4 min read
Python | Lowercase first character of String The problem of capitalizing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the first character of the string to lowercase. Let us discuss certain ways in which this can be performed. Method #1: Using string sli
4 min read
Python program to capitalize the first and last character of each word in a string In this article, we will explore how to capitalize the first and last character of each word in a string in Python. It involves processing each word to transform its first and last letters to uppercase, while making the other characters lowercase.Using List Comprehensionlist comprehension and string
2 min read
Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
Python Program for Convert characters of a string to opposite case Given a string, convert the characters of the string into the opposite case,i.e. if a character is the lower case then convert it into upper case and vice-versa. Examples: Input : geeksForgEeksOutput : GEEKSfORGeEKSInput: hello every oneOutput: HELLO EVERY ONEExample 1: Python Program for Convert ch
6 min read