Python program to remove the nth index character from a non-empty string
Last Updated :
23 Feb, 2023
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)
OutputModified 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))
OutputModified 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)
OutputModified 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.
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 mos
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 t
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
2 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 is
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 Python3 # Python3 code to demonstrate # how to remove 'n' characters from starting # of a string # Initialising strin
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