Python程序从字符串中的指定索引中删除字符

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


翻译自: https://www.includehelp.com/python/program-to-remove-a-character-from-a-specified-index-in-a-string.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值