Jaden00 2024-01-27 14:55 采纳率: 0%
浏览 1

python中格式化语言

最后一行,请问这里的格式化用f是什么意思,这个print(f"x:{x})又是指什么

img

img

  • 写回答

2条回答 默认 最新

  • 精神小黑猫 2024-01-27 15:30
    关注

    用大括号 {} 表示被替换字段,其中直接填入替换内容:
    参考链接:https://blog.csdn.net/lizhaoyi123/article/details/102731501

    >>> name = 'Eric'
    >>> f'Hello, my name is {name}'
    'Hello, my name is Eric'
    
    >>> number = 7
    >>> f'My lucky number is {number}'
    'My lucky number is 7'
    
    >>> price = 19.99
    >>> f'The price of this book is {price}'
    'The price of this book is 19.99'
    
    

    这里的print(f"x:{x})中,简单的说就是等效于 将变量x的值自动转换为str类型后将{x}替换,即:print(f"x:{x}) = print( "x:"+str(x) )

    评论

报告相同问题?

问题事件

  • 创建了问题 1月27日