Open In App

Python – Lowercase Kth Character in string

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Kth Character Lowercase
# Using lower() + string slicing
 
# initializing string
test_str = "GEEKSFORGEEKS"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 4
 
# Using lower() + string slicing
# Kth Character Lowercase
res = test_str[:K] + test_str[K].lower() + test_str[K + 1:]
 
# printing result
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




# Python3 code to demonstrate working of
# Kth Character Lowercase
# Using lower() + string slicing + lambda
 
# initializing string
test_str = "GEEKSFORGEEKS"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 4
 
# Using lower() + string slicing + lambda
# Kth Character Lowercase
res = lambda test_str: test_str[:K] + test_str[K].lower() + test_str[K + 1:] if test_str else ''
 
# printing result
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




# Python3 code to demonstrate working of
# Kth Character Lowercase
 
# initializing string
test_str = "GEEKSFORGEEKS"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = 4
 
# Kth Character Lowercase
test_str=test_str.replace(test_str[K],test_str[K].lower(),1)
 
# printing result
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




#Python3 code to demonstrate working of
#Kth Character Lowercase
#initializing string
test_str = "GEEKSFORGEEKS"
 
#printing original string
print("The original string is : " + str(test_str))
 
#initializing K
K = 4
 
#Kth Character Lowercase
res = "".join([char.lower() if i == K else char for i, char in enumerate(test_str)])
 
#printing result
print("The string after lowercasing Kth character : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


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
 
# Define the original string
test_str = "GEEKSFORGEEKS"
 
# Print the original string
print("The original string is:", test_str)
 
# Define the value of K
K = 4
 
# Use the re.sub() function to search for the Kth character in the string
# and replace it with its lowercase version
result = re.sub(test_str[K], test_str[K].lower(), test_str)
 
# Print the resulting string
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
print("The original string is:", test_str)
  
K = 4
print(kth_char_lowercase(test_str, K))
#This code is contributetd by Jyothi pinjala.


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):
    # Convert string to bytearray
    b = bytearray(s.encode())
     
    # Convert character at index k to lowercase using chr() function
    b[k] = ord(chr(b[k]).lower())
     
    # Convert bytearray back to string
    return b.decode()
 
test_str = "GEEKSFORGEEKS"
# Print the original string
print("The original string is:", test_str)
 
K = 4
print(kth_char_lowercase(test_str, K))
# This code is contributed by Jyothi pinjala.


Output

The original string is: GEEKSFORGEEKS
GEEKsFORGEEKS

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



Last Updated : 28 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads