Files
2025PY/day04/13-字符串的扩展.py
2025-05-22 16:50:44 +08:00

28 lines
925 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 一.更优雅快速的字符串格式化写法:
# 第一种字符串的写法:
# name = '张三'
# age = 18
# gender = '男'
# print('我的名字是' + name + ',我今年' + str(age) + '岁,我是' + gender + '的' )
# 第二种字符串的写法:
# name = '张三'
# age = 18
# gender = '男'
# print('我的名字是%s,我今年%s岁,我是%s的' %(name,age,gender))
# 第三种字符串的写法:使用最多的
# 格式f"内容{变量}" 的格式来快速格式化
# 注意:上面的写法不关注类型和精度
# name = '张三'
# age = 18
# gender = '男'
# print(f'我的名字是{name},我今年{age}岁,我是{gender}的')
# print(f"我的名字是{name},我今年{age}岁,我是{gender}的")
# 对表达式进行格式化
print("1 * 1的结果是:%d" %(1 * 1))
print(f"1 * 2的结果是:{1 * 2}")
print("字符串在Python中的类型名是:",type("字符串")) # 逗号分隔打印多个值