一、format
"""format 占位符"""
# 字符串中有几个 {} 占位符, 那么 format 方法里面就需要有几个变量
二、%s
%s 占位符
"""字符串中有几个 %s 占位符, 那么 %()里面就需要有几个变量"""
三、f
""" f 直接带字符串里面插入变量"""
四、示例代码如下
stu_name = '懒羊羊'
stu_age = 20
stu_nickname = '小胖子' #
print('---------------------format 占位符---------------------')
"""format 占位符"""
# 字符串中有几个 {} 占位符, 那么 format 方法里面就需要有几个变量
print('姓名: {}, 年龄: {}, 外号: {}'.format(stu_name, stu_age, stu_nickname))
print('姓名: {}'.format(stu_name))
print('---------------------%s 占位符-------------------------')
"""字符串中有几个 %s 占位符, 那么 %()里面就需要有几个变量"""
print('姓名: %s, 年龄: %s, 外号: %s' % (stu_name, stu_age, stu_nickname))
print('姓名: %s' % stu_name)
print('--------------------- f ------------------------------')
""" f 直接带字符串里面插入变量"""
print(f'姓名: {stu_name}, 年龄: {stu_age}, 外号: {stu_nickname}')
print(f'姓名: {stu_name}')
运行结果: