F' 字符串字典时语法无效

Aae*_*ech 2 python

当我尝试使用 f'string 字典时,它说语法无效。

def up_low(s):
    d={"upper":0,"lower":0}
    for c in s:
        if c.isupper():
            d["upper"]+=1
            print (f"No. of Upper case characters: {d["upper"]}")
        elif c.islower():
            d["lower"]+=1
            print (f"No. of Lower case characters:{d["lower"]}")
        else:
            pass
Run Code Online (Sandbox Code Playgroud)

它说第 6 行和第 9 行语法无效。

MrG*_*eek 5

不要"在用 括起来的字符串中使用",或者使用',或者将字符串括在 中'

f"No. of Upper case characters: {d['upper']}"
Run Code Online (Sandbox Code Playgroud)

或者:

f'No. of Upper case characters: {d["upper"]}'
Run Code Online (Sandbox Code Playgroud)