当前位置: 首页 > 工具软件 > F# > 使用案例 >

Python 的 f'' 字符串

公孙栋
2023-12-01

Python 的 f’’ 字符串

本文学习自https://blog.csdn.net/duxin_csdn/article/details/88583429

  1. 理解:

    f’’ 字符串类型与 format 函数的使用类似,但更简单

  2. format函数

    >>> name = 'Alex'
    >>> age = 25
    >>> sex = 'Male'
    >>> introducation = 'My name is {name},I am a {sex},and I am {age} years old.'.format(name=name,sex=sex,age=age)
    >>> introducation
    'My name is Alex,I am a boy,and I am 25 years old.'
    
  3. f’'字符串

    1). 使用f字符串进行拼接字符串:

    >>> introducation = f'My name is {name},I am a {sex},and I am {age} years old.'
    >>> introducation
    'My name is Alex,I am a boy,and I am 25 years old.'
    

    2). f字符串使用对象操作

    >>> info = {'name': 'Alex', 'age': 25, 'sex': 'boy'}
    >>> introducation = f'My name is {info["name"]},I am a {info["sex"]},and I am {info["age"]} years old.'
    >>>
    >>> introducation
    'My name is Alex,I am a boy,and I am 25 years old.'
    
 类似资料: