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
# 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))
OutputThe 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
OutputThe 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)
OutputThe 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.
OutputThe 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.
OutputThe original string is: GEEKSFORGEEKS
GEEKsFORGEEKS
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Lowercase first character of String
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 first character of the string to lowercase. Let us discuss certain ways in which this can be performed. Method #1: Using string sli
4 min read
Python | Return lowercase characters from given string
Sometimes, while working with strings, we are concerned about the case sensitivity of strings and might require getting just a specific case of character in a long string. Let's discuss certain ways in which only lowercase letters can be extracted from a string. Method #1: Using list comprehension +
5 min read
Python - Least Frequent Character in String
The task is to find the least frequent character in a string, we count how many times each character appears and pick the one with the lowest count.Using collections.CounterThe most efficient way to do this is by using collections.Counter which counts character frequencies in one go and makes it eas
3 min read
Python - Groups Strings on Kth character
Sometimes, while working with Python Strings, we can have a problem in which we need to perform Grouping of Python Strings on the basis of its Kth character. This kind of problem can come in day-day programming. Let's discuss certain ways in which this task can be performed. Method #1: Using loop Th
4 min read
Iterate over characters of a string in Python
In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
Python - Characters Index occurrences in String
Sometimes, while working with Python Strings, we can have a problem in which we need to check for all the characters indices. The position where they occur. This kind of application can come in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using set() + reg
6 min read
Python | Longest Run of given Character in String
Sometimes, while working with Strings, we can have a problem in which we need to perform the extraction of length of longest consecution of certain letter. This can have application in web development and competitive programming. Lets discuss certain ways in which this task can be performed. Method
6 min read
Python | Kth index character similar Strings
Sometimes, we require to get the words that have the Kth index with the specific letter. This kind of use case is quiet common in places of common programming projects or competitive programming. Letâs discuss certain shorthand to deal with this problem in Python. Method #1: Using list comprehension
3 min read
Python | Remove Kth character from strings list
Sometimes, while working with data, we can have a problem in which we need to remove a particular column, i.e the Kth character from string list. String are immutable, hence removal just means re creating a string without the Kth character. Let's discuss certain ways in which this task can be perfor
7 min read
Get Last N characters of a string - Python
We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh
2 min read