【问题标题】:How can i make user enter "string" and enter (index) and remove this (index) from "string" which he entered?我怎样才能让用户输入 \"string\" 并输入(索引)并从他输入的 \"string\" 中删除这个(索引)?
【发布时间】:2022-11-04 00:26:40
【问题描述】:

Text = input(">> Please, Enter The Text: ")
index_X = input(">> Please, Enter The index_X which you want to remove from 'Text': ")
print(Text.remove(index_X))

我尝试使用替换方法 但我认为还有另一种方法

【问题讨论】:

  • 欢迎来到 SO! python 中的input() 总是将string 作为输入。

标签: python python-3.x


【解决方案1】:

您可以获取索引之前的字符串片段,然后获取索引之后的字符串片段并将它们放在一起。

Text = input(">> Please, Enter The Text: ")
index_X = int(input(">> Please, Enter The index_X which you want to remove from 'Text': ")) #convert to int
new_text = Text[:index_X] + Text[index_X+1:]
print(new_text)

【讨论】:

  • new_text = Text[:index_X] + Text[index_X+1:] TypeError: slice indices must be integers or None or have an指数方法
猜你喜欢
  • 2015-02-07
  • 2021-09-24
  • 2022-11-01
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 2021-02-10
相关资源
最近更新 更多