【问题标题】:How to add char to unknown string length?如何将字符添加到未知字符串长度?
【发布时间】:2019-05-04 13:51:22
【问题描述】:

我有一个字符串,我知道它包含单词“python”,但我不知道该字符串有多少个字符。 我想添加“。”在“蟒蛇”之前 例如:

     str = input("enter string")
     #Here I need help
     print(str)#The output will be the user's input and "." before the word "python" let say str = "I love python" the output will be "I love .python"

【问题讨论】:

  • 你尝试过什么代码来解决它? @VictorHanukayev
  • 我不知道怎么解决
  • 看看string.splitstring.join 方法!他们将帮助您找到解决方案!
  • 您也可以使用if条件来检查python是否在用户的输入中,然后将python替换为.python

标签: python string


【解决方案1】:

我没有完全明白你的问题,但看看这个,也许它会有所帮助。

inp=input('enter input ::')  # taking user input

new_str=inp.replace('python','.python') #just replace python will .python in user input

print(new_str)

输出将是这样的:

enter input ::i love python
i love .python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2015-04-27
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多