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

问题描述 投票:1回答:2

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

示例

key = input("Enter the product :")

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

print (URL)

User input: iPhone 11

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

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

python python-3.x
2个回答
0
投票

尝试编码密钥:

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

0
投票

您可以使用字符串格式:

key = input("Enter the product :")

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

print("{}+{}".format(URL, key))
© www.soinside.com 2019 - 2024. All rights reserved.