Python - 带 .format 或 %s 的多行 raw_input

Python - multi-line raw_input with .format or %s

我想做一些功能上等同于此的事情:

my_dict = {'option1': 'VALUE1', 'option2': 'VALUE2'}
def my_func():
    menu_option = raw_input(
        "Which option would you like to configure [0]?\n"
        "[0] NO CHANGES\n"
        "[1] Option1: \t{0}\n".format(my_dict.get('option1'))
        "[2] Option2: \t{0}\n".format(my_dict.get('option2'))
    ) or "0"

my_dict = {'option1': 'VALUE1', 'option2': 'VALUE2'}
def my_func():
    menu_option = raw_input(
        "Which option would you like to configure [0]?\n"
        "[0] NO CHANGES\n"
        "[1] Option1: \t %s \n" % my_dict.get('option1')
        "[2] Option2: \t %s \n" % my_dict.get('option2')
    ) or "0"

运行 my_func() 的结果如下所示:

Which option would you like to configure [0]?
[0] NO CHANGES
[1] Option1:     VALUE1
[2] Option2:     VALUE2

我遇到无效的语法错误。有办法吗?

多行注释写成""":

my_dict = {'option1': 'VALUE1', 'option2': 'VALUE2'}
def my_func():
    menu_option = raw_input(
        """Which option would you like to configure [0]?
        [0] NO CHANGES
        [1] Option1: \t{0}
        [2] Option2: \t{1}\n""".format(my_dict.get('option1'), my_dict.get('option2'))
    ) or "0"

您在将多行字符串与 format 调用组合时使用;使用单一格式的多行字符串

menu_option = raw_input("""
    Which option would you like to configure [0]?
    [0] NO CHANGES
    [1] Option1: \t{0}
    [2] Option2: \t{1}
    """.format(my_dict.get('option1'), my_dict.get('option2'))
) or "0"

或添加连接运算符

menu_option = raw_input(
    "Which option would you like to configure [0]?\n" + \
    "[0] NO CHANGES\n" + \
    "[1] Option1: \t{0}\n".format(my_dict.get('option1') + \
    "[2] Option2: \t{0}\n".format(my_dict.get('option2')
) or "0"