Q:

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

belongs to collection: Python String Programs

0

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

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

Question:

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'

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program for adding given string with a fixe... >>
<< Python program to swap characters of a given strin...