删除特定索引处的字符 - python [英] Remove char at specific index - python

查看:84
本文介绍了删除特定索引处的字符 - python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中有两个0"(str),我只想删除索引 4 处的0"(str)

I have a string that has two "0" (str) in it and I want to remove only the "0" (str) at index 4

我曾尝试调用 .replace 但显然会删除所有0",而且我找不到可以为我删除位置 4 处的字符的函数.

I have tried calling .replace but obviously that removes all "0", and I cannot find a function that will remove the char at position 4 for me.

有人给我提示吗?

推荐答案

使用切片,重建字符串减去要删除的索引:

Use slicing, rebuilding the string minus the index you want to remove:

newstr = oldstr[:4] + oldstr[5:]

这篇关于删除特定索引处的字符 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆