Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Lowercase Kth Character in string
Next article icon

Python - Lowercase Kth Character in string

Last Updated : 28 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
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)


Next Article
Python - Lowercase Kth Character in string

M

manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences