小编Dee*_*ara的帖子

如何将f字符串与变量一起使用,而不是与字符串文字一起使用?

我想将f-string与我的string变量一起使用,而不是与使用字符串常量定义的string一起使用"..."

这是我的代码

name=["deep","mahesh","nirbhay"]
user_input = r"certi_{element}" # this string i ask from user  

for element in name:
    print(f"{user_input}")
Run Code Online (Sandbox Code Playgroud)

此代码提供输出:

certi_{element}
certi_{element}
certi_{element}
Run Code Online (Sandbox Code Playgroud)

但我想要:

certi_{deep}
certi_{mahesh}
certi_{nirbhay}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

python string string-formatting python-3.x f-string

3
推荐指数
3
解决办法
4024
查看次数

标签 统计

f-string ×1

python ×1

python-3.x ×1

string ×1

string-formatting ×1