我正在通过用户输入呈现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”
尝试编码密钥:
import urllib.parse
key = input("Enter the product :")
path = urllib.parse.quote_plus(key)
URL = "http://exmaple.com/{}".format(path)
print (URL)
您可以使用字符串格式:
key = input("Enter the product :")
URL = "http://exmaple.com/"
print("{}+{}".format(URL, key))