Python 移除字符串中的指定位置字符

Document 对象参考手册 Python3 实例

给定一个字符串,然后移除制定位置的字符:

实例

test_str = "Runoob"
 
# 输出原始字符串
print ("原始字符串为 : " + test_str)
 
# 移除第三个字符 n
new_str = ""
 
for i in range(0, len(test_str)):
    if i != 2:
        new_str = new_str + test_str[i]

print ("字符串移除后为 : " + new_str)

执行以上代码,输出结果为:

原始字符串为 : Runoob
字符串移除后为 : Ruoob

Document 对象参考手册 Python3 实例

其他扩展