Python program to remove a character from a specified index in a string

Here, we are going to learn how to remove a character from a specified index in a string using python program? By Suryaveer Singh Last updated : February 25, 2024

Here, we would learn how we can remove a character from an index in a string easily?

Problem statement

Given a string and an index, we have to remove the character from given index in the string using python program.

We are provided a non-empty string and an integer N, such that we have to return a string such that the character at the given index (N) has been removed. Where N belongs to the range from 0 to len(str)-1.

Example

remove_char ('raman', 1) = 'rman'
remove_char ('raman', 0) = 'aman'
remove_char ('raman', 4) = 'rama'

Code

def remove_char(str, n):
    front = str[:n]  # up to but not including n
    back = str[n + 1:]  # n+1 till the end of string
    return front + back

print (remove_char('IncludeHelp', 3))
print (remove_char('Hello', 4))
print (remove_char('Website', 1))

Output

IncudeHelp
Hell
Wbsite

To understand the above program, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.