Python program for removing i-th character from a string

Given the string, we have to remove the ith indexed character from the string.

In any string, indexing always start from 0. Suppose we have a string geeks then its indexing will be as –

g e e k s
0 1 2 3 4

Examples :

Input : Geek
        i = 1
Output : Gek 

Input : Peter 
        i = 4
Output : Pete

 



Approach : From the given string, i-th indexed element has to be removed. So, Split the string into two halves, before indexed character and after indexed character. Return the merged string.

Below is the implementation of above approach :

# Python3 program for removing i-th 
# indexed character from a string

# Removes character at index i
def remove(string, i): 

    # Characters before the i-th indexed
    # is stored in a variable a
    a = string[ : i] 
    
    # Characters after the nth indexed
    # is stored in a variable b
    b = string[i + 1: ]
    
    # Returning string after removing
    # nth indexed character.
    return a + b
    
# Driver Code
if __name__ == '__main__':
    
    string = "geeksFORgeeks"
    
    # Remove nth index element
    i = 5
  
    # Print the new string
    print(remove(string, i))

Output :

geeksORgeeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions