Home » Python » Python programs

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?
Submitted by Suryaveer Singh, on June 08, 2019

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'

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.