问答详情
源自:3-7 Python的字符串format

第二行那里不知道怎么错了

template = 'L{0} i{1} s{2},y{3} n{4} P{5}'

0 = 'ife'

1 = 's'

2 = 'hort'

3 = 'ou'

4 = 'eed'

5 = 'ython'

result =  template.format(0 = ife , 1 = s , 2 = hort , 3 = ou , 4 = eed , 5 = ython)

print(result)


提问者:weixin_慕丝9501451 2021-09-02 19:44

个回答

  • 鹿鸣ei
    2021-09-02 20:12:34

    变量不能以数字开头,所以'0''1'……这些数字不能作为变量,改成这样就可以了:

    template = 'L{a0} i{a1} s{a2},y{a3} n{a4} P{a5}'

    a0 = 'ife'

    a1 = 's'

    a2 = 'hort'

    a3 = 'ou'

    a4 = 'eed'

    a5 = 'ython'

    result =  template.format(a0 = 'ife' , a1 = 's' , a2 = 'hort' , a3 = 'ou' , a4 = 'eed' , a5 = 'ython')

    print(result)

    或者不单独定义变量也可以:

    template = 'L{0} i{1} s{2},y{3} n{4} P{5}'

    result =  template.format('ife','s','hort','ou','eed','ython')

    print(result)


    下载视频