Python 将字典的值注入到打印格式中
如何在Python中使用字符串插值将字典的值注入字符串Python 将字典的值注入到打印格式中,python,string-formatting,Python,String Formatting,如何在Python中使用字符串插值将字典的值注入字符串 airports = {"key":"value"} message = f"{airports["key"]}" // this gives a syntax error 使用Python 3.7时,需要对字符串使用单引号 airports = {"key":"value"} message = f'{airports["key"]}' 实现这一目标的另一个途径是 airports = {"key":"value"} valTo
airports = {"key":"value"}
message = f"{airports["key"]}" // this gives a syntax error
使用Python 3.7时,需要对字符串使用单引号
airports = {"key":"value"}
message = f'{airports["key"]}'
实现这一目标的另一个途径是
airports = {"key":"value"}
valToPrint = airports["key"]
message = f'{valToPrint}'
您为什么不使用
str.format
notation?一种方法:f“{airports['key']}”
。With format():message='{key}'。格式(**airports)