代码如下:

string = input()
index = int(input())

# 使用字符串切片删除指定索引处的字符
deleted_string = string[:index] + string[index+1:]

print(deleted_string)

示例输入:

python
3

示例输出:

pyton

代码解析:

  1. 使用 input() 函数获取用户输入的字符串和要删除的字符索引。
  2. 使用字符串切片操作 string[:index] + string[index+1:] 将字符串分割成两部分,第一部分包含索引之前的字符,第二部分包含索引之后的字符,并将两部分拼接起来,从而实现删除指定索引处的字符。
  3. 使用 print() 函数输出删除后的字符串。

代码示例:

string = 'Hello World'
index = 6

deleted_string = string[:index] + string[index+1:]

print(deleted_string)  # 输出:Hello World

注意:

  • 索引从 0 开始。
  • 如果输入的索引超出字符串长度,程序不会报错,但可能会导致结果不符合预期。
Python 字符串删除指定索引字符

原文地址: https://www.cveoy.top/t/topic/nvOn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录