Python – Uppercase Nth character
Last Updated :
09 Feb, 2023
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 Nth character of the string to uppercase. 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 uppercase Nth character.
Python3
test_str = "GeeksforGeeks"
print ( "The original string is : " + str (test_str))
N = 4
res = test_str[:N] + test_str[N].upper() + test_str[N + 1 :]
print ( "The string after uppercasing Nth character : " + str (res))
|
Output :
The original string is : GeeksforGeeks
The string after uppercasing Nth character : GeekSforGeeks
Method #2: Using lambda + string slicing + upper() The recipe of lambda function has to be added if we need to perform the task of handling None values or empty strings as well, and this becomes essential to handle edge cases.
Python3
test_str = "GeeksforGeeks"
print ( "The original string is : " + str (test_str))
N = 4
res = lambda test_str: test_str[:N] + test_str[N].upper() + test_str[N + 1 :] if test_str else ''
print ( "The string after uppercasing initial character : " + str (res(test_str)))
|
Output :
The original string is : GeeksforGeeks
The string after uppercasing Nth character : GeekSforGeeks
Method #3 : Using replace() and upper() methods
Python3
test_str = "GeeksforGeeks"
print ( "The original string is : " + str (test_str))
N = 4
test_str = test_str.replace(test_str[N],test_str[N].upper(), 1 )
print ( "The string after uppercasing Nth character : " + str (test_str))
|
Output
The original string is : GeeksforGeeks
The string after uppercasing Nth character : GeekSforGeeks
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Without upper() method
Python3
test_str = "GeeksforGeeks"
print ( "The original string is : " + str (test_str))
N = 4
res = ""
loweralphabets = "abcdefghijklmnopqrstuvwxyz"
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range ( 0 , len (test_str)):
if (i = = N):
res + = upperalphabets[loweralphabets.index(test_str[i])]
else :
res + = test_str[i]
print ( "The string after uppercasing Nth character : " + str (res))
|
Output
The original string is : GeeksforGeeks
The string after uppercasing Nth character : GeekSforGeeks
Method #5 : Using maketrans() and translate()
Python3
test_str = "GeeksforGeeks"
print ( "The original string is : " + str (test_str))
N = 4
lower_uppers = str .maketrans( "abcdefghijklmnopqrstuvwxyz" , "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )
res = test_str[:N] + test_str[N].translate(lower_uppers) + test_str[N + 1 :]
print ( "The string after uppercasing Nth character : " + str (res))
|
Output
The original string is : GeeksforGeeks
The string after uppercasing Nth character : GeekSforGeeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: using list comprehension
Python3
test_str = "GeeksforGeeks"
print ( "The original string is : " + str (test_str))
N = 4
res = ''.join([char.upper() if i = = N else char for i, char in enumerate (test_str)])
print ( "The string after uppercasing Nth character : " + str (res))
|
Output
The original string is : GeeksforGeeks
The string after uppercasing Nth character : GeekSforGeeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#7:using re module
Python3
import re
test_str = "GeeksforGeeks"
N = 4
res = re.sub(f "^{test_str[:N]}{test_str[N]}" , f "{test_str[:N]}{test_str[N].upper()}" , test_str)
print (res)
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#7:using string concatenation
Python3
test_str = "GeeksforGeeks"
print ( "The original string is : " + str (test_str))
N = 4
res = test_str[:N] + test_str[N].upper() + test_str[N + 1 :]
print ( "The string after uppercasing Nth character : " + str (res))
|
Output
The original string is : GeeksforGeeks
The string after uppercasing Nth character : GeekSforGeeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...