python - 如何在两个单词之间添加+号

标签 python python-3.x

我正在通过用户输入呈现 URL。如果用户输入一个包含两个单词的字符串,我想打印中间带有 + 的单词

示例

key = input("Enter the product :")

URL = "http://exmaple.com/"

print (URL)

User input: iPhone 11

对于上面的代码,我得到的 URL 为“http://exmaple.com/iphone 11”

但我想将 URL 打印为“http://exmaple.com/iphone+11

最佳答案

尝试对 key 进行编码:

import urllib.parse
key = input("Enter the product :")
path = urllib.parse.quote_plus(key)
URL = "http://exmaple.com/{}".format(path)
print (URL)

关于python - 如何在两个单词之间添加+号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60902243/

相关文章:

python - "if variable not in list"一直不工作返回真

python - 如何抑制python中的错误消息?

python - 如何为WhatsApp .crypt12格式生成页眉和页脚?

python - 如何在代码迭代时追加到列表并保存列表(对于 twitch 聊天机器人)

python - Python 命令提示符中的制表符/缩进错误

python - 删除具有空值的已抓取数据

python - 根据字符在数据框中创建新记录

python - 输出金字塔的高度

python-3.x - python oracle where子句包含大于比较的日期

python - 当尝试设置运行计划时,类立即调用方法。函数没有发生