Open In App

How to capitalize first character of string in Python

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 performed using the upper method which uppercases the characters provided to it and slicing can be used to add the remaining string after the lowercase first character. 

Python3




# Python3 code to demonstrate working of
# Initial character upper case
# Using upper() + string slicing
 
# initializing string
test_str = "geeksforgeeks"
# printing original string
print("The original string is : " + str(test_str))
 
# Using upper() + string slicing
# Initial character upper case
res = test_str[0].upper() + test_str[1:]
 
# printing result
print("The string after uppercasing initial character : " + str(res))


Output : 

The original string is : geeksforgeeks
The string after uppercasing initial character : Geeksforgeeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using capitalize() We can use the inbuilt method to perform this task. This method is recommended to solve this problem and performs the task of converting to upper case internally. 

Python3




# Python3 code to demonstrate working of
# Initial character upper case
# Using capitalize() + string slicing
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using capitalize() + string slicing
# Initial character upper case
res = test_str.capitalize()
 
# printing result
print("The string after uppercasing initial character : " + str(res))


Output : 

The original string is : geeksforgeeks
The string after uppercasing initial character : Geeksforgeeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using str.title() method 

We can use string title function to perform this task. Title function convert the first letter of word to upper case and remaining to lower case. 

Python3




# Python3 code to demonstrate working of
# Initial character upper case
# Using title function of string
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using str.title()
# Initial character upper case
res = test_str.title()
 
# printing result
print("The string after uppercasing initial character : " + str(res))


Output

The original string is : geeksforgeeks
The string after uppercasing initial character : Geeksforgeeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4 : Using replace() method

Python3




# Python3 code to demonstrate working of
# Initial character upper case
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Initial character upper case
res = test_str.replace(test_str[0],test_str[0].upper(),1)
 
# printing result
print("The string after uppercasing initial character : " + str(res))


Output

The original string is : geeksforgeeks
The string after uppercasing initial character : Geeksforgeeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5 : Using slicing and index() method

Python3




# Python3 code to demonstrate working of
# Initial character upper case
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Initial character upper case
lower="abcdefghijklmnopqrstuvwxyz"
upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res=upper[lower.index(test_str[0])]+test_str[1:]
 
# printing result
print("The string after uppercasing initial character : " + str(res))


Output

The original string is : geeksforgeeks
The string after uppercasing initial character : Geeksforgeeks

Time Complexity: O(n), where n is the length of the string ‘test_str’.
Auxiliary Space: O(n), where n is the length of the string ‘res’.

Method #6: Using join() and split() methods

This method involves splitting the string into a list of words, capitalizing the first letter of each word using the upper() method, and then joining the words back together using the join() method.

Step-by-step approach:

  • Initialize the string.
  • Split the string into a list of words using the split() method.
  • Use a list comprehension to capitalize the first letter of each word using the upper() method.
  • Join the capitalized words back together using the join() method with a space as the separator.
  • Print the final result

Below is the implementation of the above approach:

Python3




# initializing string
test_str = "geeksforgeeks"
# printing original string
print("The original string is : " + str(test_str))
 
# Using join() and split() methods
# Initial character upper case
res = ' '.join([word.capitalize() for word in test_str.split()])
 
# printing result
print("The string after uppercasing initial character : " + str(res))


Output

The original string is : geeksforgeeks
The string after uppercasing initial character : Geeksforgeeks

Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(n), where n is the length of the string.

Method #7: Using ord() and chr() functions

Initialize the string.
Extract the first character of the string using indexing and assign it to a variable.
Convert the ASCII value of the first character to its uppercase equivalent using the ord() function to get its ASCII value, subtracting 32 from it, and converting it back to its character form using the chr() function.
Concatenate the uppercase character with the rest of the string using string slicing.
Print the resulting string.

Python3




# Python3 code to demonstrate working of
# Initial character upper case
 
# initializing string
test_str = "geeksforgeeks"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using ord() and chr() functions
upper_char = chr(ord(test_str[0])-32)
res = upper_char + test_str[1:]
 
# printing result
print("The string after uppercasing initial character : " + str(res))


Output

The original string is : geeksforgeeks
The string after uppercasing initial character : Geeksforgeeks

Time complexity: O(1) 
Auxiliary space: O(N) .



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads