Python program for removing nth character from a string


In this article, we will learn about the solution to the problem statement given below −

Problem statement − We are given a string, we have to remove the ith indexed character from the given string and display it.

In any string in Python, indexing always starts from 0. Suppose we have a string “tutorialspoint” then its indexing will be done as shown below −

T u t o r i a l s p  o  i  n  t
0 1 2 3 4 5 6 7 8 9 10 11 12 13

Now let’s see the Python script for solving the statement −

Example

 Live Demo

def remove(string, i):
   # slicing till ith character
   a = string[ : i]
   # slicing from i+1th index
   b = string[i + 1: ]
   return a + b
# Driver Code
if __name__ == '__main__':
   string = "Tutorialspoint"
   # Remove nth index element
   i = 8
   print(remove(string, i))

Output

Tutorialpoint

Algorithms − From the given input string, i-th indexed element has to be popped. So, Split the string into two parts, before indexed character, and after indexed character thereby leaving the ith character Return the merged string.

Here we have three variables declared in global scope as shown below −

Conclusion

In this article, we learned about the removal of ith character from a given input string in Python 3.x or earlier

Updated on: 11-Sep-2019

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements