Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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 program to remove last N characters from a string
Next article icon

Python program to remove the nth index character from a non-empty string

Last Updated : 23 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a String, the task is to write a Python program to remove the nth index character from a non-empty string

Examples:

Input: str = "Stable" Output: Modified string after removing  4 th character  Stabe   Input: str = "Arrow" Output: Modified string after removing  4 th character  Arro

The first approach uses a new string variable for storing the modified string. We keep a track of the characters of the string and as soon as we encounter a character at nth index, we don’t copy it to the modified string variable. Else, we copy it to a new variable. 

Python3




# declaring a string variable
str = "Geeksforgeeks is fun."
 
# index to remove character at
n = 4
 
# declaring an empty string variable for storing modified string
modified_str = ''
 
# iterating over the string
for char in range(0, len(str)):
 
    # checking if the char index is equivalent to n
    if(char != n):
        # append original string character
        modified_str += str[char]
 
print("Modified string after removing ", n, "th character ")
print(modified_str)
 
 

Output:

Modified string after removing  4 th character   Geekforgeeks is fun.

Time Complexity = O(n), where n is the length of the string. 

Auxiliary Space: O(n)

The second approach uses the idea of extraction of a sequence of characters within a range of index values. The syntax used in Python is as follows : 

string_name[start_index : end_index] 
– extracts the characters starting at start_index 
and less than end_index, that is, up to end_index-1. 
If we don’t specify the end_index, it computes till the length of the string.

Therefore, we extract all the characters of a string in two parts, first until nth index and the other beginning with n+1th index. We then append these two parts together. 

Python3




# declaring a string variable
str = "Geeksforgeeks is fun."
 
# index to remove character at
n = 8
 
# extracts 0 to n-1th index
first_part = str[0:n]
 
# extracts characters from n+1th index until the end
second_part = str[n+1:]
print("Modified string after removing ", n, "th character ")
 
# combining both the parts together
print(first_part+second_part)
 
 

Output:

Modified string after removing  8 th character   Geeksforeeks is fun.

Time Complexity = O(n), where n is the length of the string.

Auxiliary Space = O(n)

Third Approach :

Python3




#Python program to remove nth index character from non-empty string
# declaring a string variable
str = "Geeksforgeeks is fun."
 
# index to remove character at
n = 8
print("Modified string after removing ", n, "th character ")
str=str.replace(str[n],"",1)
print(str)
 
 
Output
Modified string after removing  8 th character  Geeksforeeks is fun.

Fourth Approach: Type casting into list and del to remove character

Python3




#input string
str1 = "Geeksforgeeks is fun."
# index to remove character at
n=4
#type casting
str1=list(str1)
print("Modified string after removing ", n, "th character ")
del str1[n]
#printing as list
print(''.join(str1))
 
 
Output
Modified string after removing  4 th character  Geekforgeeks is fun.

Time Complexity = O(n), where n is the length of the string.

Auxiliary Space = O(n)

Approach using islice

Step-by-step algorithm to remove the nth index character from a non-empty string using the islice approach:

Import the islice module from itertools.
Declare a non-empty string.
Declare an integer n that represents the index of the character to remove.
Use the islice function to extract all characters from the beginning of the string to the (n-1)th character, then join them using the join method and store the result.
Use the islice function again to extract all characters from the (n+1)th character to the end of the string, then join them using the join method.
Add the two strings from steps 4 and 5 together and store the result.
Print the modified string.

Python3




from itertools import islice
 
#declaring a string variable
str = "Geeksforgeeks is fun."
 
#index to remove character at
n = 8
 
#using islice to extract characters from the string
result = ''.join(islice(str, 0, n)) + ''.join(islice(str, n+1, len(str)))
 
print("Modified string after removing ", n, "th character ", result)
 
 
Output
Modified string after removing  8 th character  Geeksforeeks is fun. 

Time complexity: O(n), where n is the length of the string. This is because the islice function must be called twice, and each call takes O(n) time in the worst case.
Auxiliary space complexity: O(n), where n is the length of the string. This is because we create two new strings, each with a length of up to n-1, and then join them together to create the modified string.



Next Article
Python program to remove last N characters from a string

Y

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

Similar Reads

  • Python program to remove last N characters from a string
    In this article, we’ll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions. Using String SlicingString slicing is one of the simplest and mo
    2 min read
  • Python program for removing i-th character from a string
    In this article, we will explore different methods for removing the i-th character from a string in Python. The simplest method involves using string slicing. Using String SlicingString slicing allows us to create a substring by specifying the start and end index. Here, we use two slices to exclude
    2 min read
  • Python program to extract characters in given range from a string list
    Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best",
    4 min read
  • Python program to print k characters then skip k characters in a string
    Given a String, extract K characters alternatively. Input : test_str = 'geeksgeeksisbestforgeeks', K = 4 Output : geekksisforg Explanation : Every 4th alternate range is sliced. Input : test_str = 'geeksgeeksisbest', K = 4 Output : geekksis Explanation : Every 4th alternate range is sliced. Method #
    5 min read
  • Remove Multiple Characters from a String in Python
    Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Let’s explore the different ways to achieve this in det
    3 min read
  • Python Program for Remove leading zeros from a Number given as a string
    Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0". Examples: Input: str = "0001234" Output: 1234 Explanation: Removal of leading substring "000" modifies the string to "1234". Hence, the final answer
    3 min read
  • Python | Ways to remove n characters from start of given string
    Given a string and a number 'n', the task is to remove a string of length 'n' from the start of the string. Let's a few methods to solve the given task. Method #1: Using Naive Method C/C++ Code # Python3 code to demonstrate # how to remove 'n' characters from starting # of a string # Initialising st
    3 min read
  • Python | Remove given character from Strings list
    Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
    8 min read
  • Python program to Extract string till first Non-Alphanumeric character
    Given a string, extract all the alphanumerics before 1st occurrence of non-alphanumeric. Input : test_str = 'geek$s4g!!!eeks' Output : geek Explanation : Stopped at $ occurrence. Input : test_str = 'ge)eks4g!!!eeks' Output : ge Explanation : Stopped at ) occurrence. Method #1 : Using regex + search(
    4 min read
  • Python Program to remove a specific digit from every element of the list
    Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list. Examples: Input : test_list = [333, 893, 1948, 34, 2346], K = 3 Output : ['', 89, 1948, 4, 246] Explanation : All occurrenc
    7 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