How to capitalize first character of string in Python
Last Updated :
05 May, 2023
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
test_str = "geeksforgeeks"
print ( "The original string is : " + str (test_str))
res = test_str[ 0 ].upper() + test_str[ 1 :]
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
test_str = "geeksforgeeks"
print ( "The original string is : " + str (test_str))
res = test_str.capitalize()
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
test_str = "geeksforgeeks"
print ( "The original string is : " + str (test_str))
res = test_str.title()
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
test_str = "geeksforgeeks"
print ( "The original string is : " + str (test_str))
res = test_str.replace(test_str[ 0 ],test_str[ 0 ].upper(), 1 )
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
test_str = "geeksforgeeks"
print ( "The original string is : " + str (test_str))
lower = "abcdefghijklmnopqrstuvwxyz"
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res = upper[lower.index(test_str[ 0 ])] + test_str[ 1 :]
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
test_str = "geeksforgeeks"
print ( "The original string is : " + str (test_str))
res = ' ' .join([word.capitalize() for word in test_str.split()])
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
test_str = "geeksforgeeks"
print ( "The original string is : " + str (test_str))
upper_char = chr ( ord (test_str[ 0 ]) - 32 )
res = upper_char + test_str[ 1 :]
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
Share your thoughts in the comments
Please Login to comment...