Write a Python program to remove the nth index character from a nonempty string


This program is written in the Python programming language and it removes a specific character from a given string. It does this using a function named "remove_char" which takes two parameters: "str" and "n".

  • "str" is the input string, and "n" is the zero-based index of the character to be removed from the string.
  • The function splits the input string into two parts using slicing:
    • The first part is assigned to the variable "a" and includes all characters of the string up to (but not including) the character at index "n".
    • The second part is assigned to the variable "b" and includes all characters of the string after (and not including) the character at index "n".
  • The function then concatenates the two parts back together and returns the result.

Finally, the program calls the "remove_char" function twice with different input parameters and prints the results to the console.

Source Code

def remove_char(str, n):
      a = str[:n] 
      b = str[n+1:]
      return a + b
print(remove_char('Tutor Joes', 2))
print(remove_char('Tutor Joes', 7))

Output

Tuor Joes
Tutor Jes


Example Programs