write a python program to remove the nth index character from a non-empty string

Hi in this article you will learn a Python program that takes a string and an index value as input. The output of this program will be a new string by skipping the index value from the given input string.


write a python program to remove the nth index character from a non-empty string




write a python program to remove the nth index character from a non-empty string


text = input("Enter a String : ")
n = int(input("Enter nth Index that needs to be removed : "))

new_string = ""
for index, letter in enumerate(text):
   if index == n:
       continue
   new_string += letter

print("The new string after removing the nth index character is : ", new_string)




Explanation
=====================

Lets ask the user to enter a string and an index value.

Now  take a  new empty string,  using the for loop enumerate the given string to get an index as well as a letter.

Check if the index value is equal to n then use the continue statement to keep the letter and keep adding the letter to the new string.



Output
============

Enter a String : code with tj
Enter nth Index that needs to be removed : 2
The new string after removing the nth index character is :  coe with tj



Output 2
================

Enter a String : hello, how are you
Enter nth Index that needs to be removed : 8
The new string after removing the nth index character is :  hello, how are you




Conclusion
==================

In the above program I am using the for loop,  enumerate and continuous statements  to remove the nth index letter from a given string.

After removing the end index I'll be printing the resulting string to the standard output using the print function call.

Execute the above Python program by providing a random string as input to it and index value,  note down the results.

Comment down below if you have any suggestions to improve the above Python program

Post a Comment

0 Comments