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.

advertisement
advertisement
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

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.