f-strings 在字符串里加入表达式 (Python3.6)

savedir=f"save/{args.savedir}"

formatted string literals, 以 f 开头,包含的{}表达式在程序运行时会被表达式的值代替。上面的代码存储了命令行args.savedir传入进来的路径。

再来一个例子:

>>> name = "yly"
>>> Education = "PhD"
>>> f"I'm {name}, and I'm a {Education}."
'I'm yly, and I'm a PhD.'

再次感叹Python语法简洁优美!!!

Ref: https://realpython.com/python-f-strings/  &  https://cito.github.io/blog/f-strings/

猜你喜欢

转载自blog.csdn.net/Cxiazaiyu/article/details/81210964