Python – Lowercase Kth Character in string
Last Updated :
28 Apr, 2023
The problem of lowercasing 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 string to lowercase. Let’s discuss certain ways in which this can be performed.
Method #1 : Using string slicing + lower() This task can easily be performed using the lower method which lowercases the characters provided to it and slicing can be used to add the remaining string after the lowercase Nth character.
Python3
test_str = "GEEKSFORGEEKS"
print ( "The original string is : " + str (test_str))
K = 4
res = test_str[:K] + test_str[K].lower() + test_str[K + 1 :]
print ( "The string after lowercasing Kth character : " + str (res))
|
Output :
The original string is : GEEKSFORGEEKS
The string after lowercasing Kth character : GEEKsFORGEEKS
Method #2 : Using lambda + string slicing + lower() 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))
K = 4
res = lambda test_str: test_str[:K] + test_str[K].lower() + test_str[K + 1 :] if test_str else ''
print ( "The string after lowercasing initial character : " + str (res(test_str)))
|
Output :
The original string is : GEEKSFORGEEKS
The string after lowercasing Kth character : GEEKsFORGEEKS
Method #3 : Using replace() and lower() methods
Python3
test_str = "GEEKSFORGEEKS"
print ( "The original string is : " + str (test_str))
K = 4
test_str = test_str.replace(test_str[K],test_str[K].lower(), 1 )
print ( "The string after lowercasing Kth character : " + str (test_str))
|
Output
The original string is : GEEKSFORGEEKS
The string after lowercasing Kth character : GEEKsFORGEEKS
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Another approach using list comprehension and join()
Python3
test_str = "GEEKSFORGEEKS"
print ( "The original string is : " + str (test_str))
K = 4
res = "".join([char.lower() if i = = K else char for i, char in enumerate (test_str)])
print ( "The string after lowercasing Kth character : " + str (res))
|
Output
The original string is : GEEKSFORGEEKS
The string after lowercasing Kth character : GEEKsFORGEEKS
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: using re module
Python3
import re
test_str = "GEEKSFORGEEKS"
print ( "The original string is:" , test_str)
K = 4
result = re.sub(test_str[K], test_str[K].lower(), test_str)
print ( "The string after lowercasing Kth character:" , result)
|
Output
The original string is: GEEKSFORGEEKS
The string after lowercasing Kth character: GEEKsFORGEEKs
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: using String concatenation and slicing:
Python3
def kth_char_lowercase(s, k):
return s[:k] + s[k].lower() + s[k + 1 :]
test_str = "GEEKSFORGEEKS"
print ( "The original string is:" , test_str)
K = 4
print (kth_char_lowercase(test_str, K))
|
Output
The original string is: GEEKSFORGEEKS
GEEKsFORGEEKS
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using bytearray and chr() functions:
Python3
def kth_char_lowercase(s, k):
b = bytearray(s.encode())
b[k] = ord ( chr (b[k]).lower())
return b.decode()
test_str = "GEEKSFORGEEKS"
print ( "The original string is:" , test_str)
K = 4
print (kth_char_lowercase(test_str, K))
|
Output
The original string is: GEEKSFORGEEKS
GEEKsFORGEEKS
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...