Friday 1 January 2021

Python Program to Remove the nth Index Character from a Non-Empty String

 This is a Python Program to remove the nth index character from a non-empty string.

Problem Description

The program takes a string and removes the nth index character from the non-empty string.

Problem Solution

1. Take a string from the user and store it in a variable.
2. Take the index of the character to remove.
3. Pass the string and the index as arguments to a function named remove.
4. In the function, the string should then be split into two halves before the index character and after the index character.
5. These two halves should then be merged together.
6. Print the modified string.
7. Exit.

Program/Source Code

Here is source code of the Python Program to remove the nth index character from a non-empty string. The program output is also shown below.

def remove(string, n):  
      first = string[:n]   
      last = string[n+1:]  
      return first + last
string=raw_input("Enter the sring:")
n=int(input("Enter the index of the character to remove:"))
print("Modified string:")
print(remove(string, n))
Program Explanation

1. User must enter a string and store it in a variable.
2. User must also enter the index of the character to remove.
3. The string and the index of the character to remove are passed as arguments to the remove function.
4. In the function, the string is split into two halves before the index character and after the index character.
5. The first half and the last half is then merged together using the ‘+’ operator.
6. The modified string is printed.

Runtime Test Cases
 
Case 1:
Enter the sring:Hello
Enter the index of the character to remove:3
Modified string:
Helo
 
Case 2:
Enter the sring:Checking
Enter the index of the character to remove:4
Modified string:
Checing

No comments:

Post a Comment