我正在通过用户输入呈现 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/