Given a string and an index, we have to remove the character from given index in the string using python program.
给定一个字符串和一个索引,我们必须使用python程序从字符串中给定的索引中删除字符。
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.
我们提供了一个非空字符串和一个整数N ,以便我们必须返回一个字符串,以便删除给定索引(N)处的字符 。 其中N属于从0到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