Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Uppercase Nth character

Python – Uppercase Nth character

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




# Python3 code to demonstrate working of
# Uppercase Nth character
# Using upper() + string slicing
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing N
N = 4
 
# Using upper() + string slicing
# Uppercase Nth character
res = test_str[:N] + test_str[N].upper() + test_str[N + 1:]
 
# printing result
print("The string after uppercasing Nth character : " + str(res))


Output : 

The original string is : Lazyroar
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




# Python3 code to demonstrate working of
# Uppercase Nth character
# Using upper() + string slicing + lambda
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing N
N = 4
 
# Using upper() + string slicing + lambda
# Uppercase Nth character
res = lambda test_str: test_str[:N] + test_str[N].upper() + test_str[N + 1:] if test_str else ''
 
# printing result
print("The string after uppercasing initial character : " + str(res(test_str)))


Output : 

The original string is : Lazyroar
The string after uppercasing Nth character : GeekSforGeeks

Method #3 : Using replace() and upper()  methods

Python3




# Python3 code to demonstrate working of
# Uppercase Nth character
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing N
N = 4
 
# Uppercase Nth character
test_str = test_str.replace(test_str[N],test_str[N].upper(),1)
 
# printing result
print("The string after uppercasing Nth character : " + str(test_str))


Output

The original string is : Lazyroar
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




# Python3 code to demonstrate working of
# Uppercase Nth character
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing N
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]
# printing result
print("The string after uppercasing Nth character : " + str(res))


Output

The original string is : Lazyroar
The string after uppercasing Nth character : GeekSforGeeks

Method #5 : Using maketrans() and translate()

Python3




# Python3 code to demonstrate working of
# Uppercase Nth character
# Using maketrans() and translate()
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing N
N = 4
 
# Uppercase Nth character using maketrans() and translate()
lower_uppers = str.maketrans("abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
res = test_str[:N] + test_str[N].translate(lower_uppers) + test_str[N + 1:]
 
# printing result
print("The string after uppercasing Nth character : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : Lazyroar
The string after uppercasing Nth character : GeekSforGeeks

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

Method #6: using list comprehension 

Python3




# This code uses list comprehension to uppercase the Nth character of a given string.
 
# initialize the string
test_str = "Lazyroar"
 
# print the original string
print("The original string is : " + str(test_str))
 
# initialize the Nth character to be uppercased
N = 4
 
# create a new string with the Nth character uppercased
res = ''.join([char.upper() if i == N else char for i, char in enumerate(test_str)])
 
# print the result
print("The string after uppercasing Nth character : " + str(res))


Output

The original string is : Lazyroar
The string after uppercasing Nth character : GeekSforGeeks

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

Method#7:using re module 

Python3




import re
 
# Define the string to be tested
test_str = "Lazyroar"
 
# Define the number N
N = 4
 
# Replace the Nth character of the string with its uppercase
# f"^{test_str[:N]}{test_str[N]}" - regular expression pattern to match the string starting with first N characters, followed by the Nth character
# f"{test_str[:N]}{test_str[N].upper()}" - replacement pattern, which is the first N characters followed by the Nth character in uppercase
# test_str - the string to be modified
res = re.sub(f"^{test_str[:N]}{test_str[N]}", f"{test_str[:N]}{test_str[N].upper()}", test_str)
 
# Print the result
print(res)


Output

GeekSforGeeks

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

Method#7:using string concatenation

Python3




# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing N
N = 4
 
# Using string concatenation
res = test_str[:N] + test_str[N].upper() + test_str[N+1:]
 
# printing result
print("The string after uppercasing Nth character : " + str(res))
#this code is contributed by Asif_Shaik


Output

The original string is : Lazyroar
The string after uppercasing Nth character : GeekSforGeeks

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

RELATED ARTICLES

Most Popular

Recent Comments