Python – Lowercase Kth Character in string
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 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)) |
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 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))) |
The original string is : GEEKSFORGEEKS The string after lowercasing Kth character : GEEKsFORGEEKS