Python program for removing n-th character from a string?



The problem statement is to remove the nth character from the given string. Let's understand with an example. Consider my_string = "Tutorialspoint".

The given value of n is 6; then we need to remove the 5th character from the string. Because the index value starts from zero. And it should result in "Tutorialspoint".

Removing nth Character using a for Loop

The for loop can be used to remove the n-th character from a string. In this approach, we iterate through each index of the string and print the characters. If the index value is equal to the given (n-1)th value, we need to skip the condition.

Example

In the following example, we have removed the n-th character from a given string by iterating through a for loop -

my_str = "Tutorialspoint"
print("Given string :",my_str)
n=5
print("n-th character to be removed :",n)
print("The string after removing n-th character : ", end="")
for i in range(0,len(my_str)):
   if i==(n-1):
      continue
   print(my_str[i], end='')
        

Following is the output of the above code -

Given string : Tutorialspoint
n-th character to be removed : 5
The string after removing n-th character : Tutoialspoint

Removing nth Character using String Slicing

The concatenation of the string slicing() up to n-1 and from n removes the nth character of the string.

Example

In the following example, we have removed the 5th character from the "Tutorialspoint" using string slicing -

my_str = "Welcome to Python Tutorial"
print("Given string :",my_str)
n=5
print("n-th character to be removed :",n)
print("The string after removing n-th character : ", end="")
print(my_str[:n-1]+my_str[n:])

Following is the output of the above code -

Given string : Welcome to Python Tutorial
n-th character to be removed : 5
The string after removing n-th character : Welcme to Python Tutorial

Removing nth Character using replace() Method

The Python string replace() method replaces all occurrences of one substring in a string with another substring. To replace the nth character of the given string, we have replaced that character with an empty string.

Example

In the following example, we have removed the 6th character from the "Welcome to Python Tutorial" using replace() method -

my_str = "Welcome to Python Tutorial"
print("Given string :",my_str)
n=6
print("n-th character to be removed :",n)
print("The string after removing n-th character : ", end="")
my_str=my_str.replace(my_str[n-1],"",1)
print(my_str)

Following is the output of the above code -

Given string : Welcome to Python Tutorial
n-th character to be removed : 6
The string after removing n-th character : Welcoe to Python Tutorial
Updated on: 2025-06-23T19:30:06+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements