Python program to remove the nth index character from a non-empty string
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
str = "Geeksforgeeks is fun."
n = 4
modified_str = ''
for char in range ( 0 , len ( str )):
if (char ! = n):
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
str = "Geeksforgeeks is fun."
n = 8
first_part = str [ 0 :n]
second_part = str [n + 1 :]
print ( "Modified string after removing " , n, "th character " )
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
str = "Geeksforgeeks is fun."
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
str1 = "Geeksforgeeks is fun."
n = 4
str1 = list (str1)
print ( "Modified string after removing " , n, "th character " )
del str1[n]
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
str = "Geeksforgeeks is fun."
n = 8
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.
Last Updated :
23 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...