要在f-string中格式化URL,可以使用以下方法:
url = f"http://example.com/{path}/{id}"
import urllib.parse
path = "path with spaces"
id = 12345
path_encoded = urllib.parse.quote(path)
url = f"http://example.com/{path_encoded}/{id}"
在上述示例中,使用urllib.parse.quote()函数对包含空格的路径进行了URL编码。
请注意,这只是一种基本的方法来格式化URL,具体取决于URL的特定要求和所使用的编程语言。