Open In App

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

Improve
Improve
Like Article
Like
Save
Share
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.



Last Updated : 23 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads