【问题标题】:Why does the Syntaxerror f-string: expecting '}' happening in my code?为什么 Syntaxerror f-string: E​​xpecting '}' 在我的代码中发生?
【发布时间】:2021-06-01 00:07:47
【问题描述】:

这是发生错误的地方

print(f'Hey, {Username}, your password is {'*' * Password}. The password is **{len(Password)}** long.')

【问题讨论】:

标签: python-3.x


【解决方案1】:

那是因为您对主字符串以及'*' 使用单引号。

Python 将其解释为 -

print( f'Hey, {Username}, your password is {'# string ends here.
*' * Password}. The password is **{len(Password)}** long.')

最后一个括号不匹配,因此出现错误。

快速解决方法是在其中一个字符串中使用双引号。

print(f'Hey, {Username}, your password is {"*" * Password}. The password is **{len(Password)}** long.')

【讨论】:

    猜你喜欢
    • 2020-11-27
    • 2022-01-24
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多