day3----输出之格式化输出
格式化输出
-
1、%s、%d
%s:可以接收任意类型的变量
%d:只能接收数字类型
按照传递顺序,传值
-
2. .format
-
3.-string #python3.6以后才有的特性
-
#格式: my name is Nick, my age is 18. %s的使用方法 name = input('请输入姓名:') age = input('请输入年龄:') print('my name is %s, my age is %s.'%(name, age))
-
print("He is %d years old."%(18)) # %d 打印整数 print("His height is %f m."%(1.85)) # %f 打印浮点数 print("His height is %.2f m."%(1.85)) # %.2f 打印浮点数保留2位小数 print("Name:%10s Age:%4d Height:%6.2f"%("Nick", 18, 1.85)) # 指定占位符宽度 print("Name:%-10s Age:%-4d Height:%-6.2f"%("Nick", 18, 1.85)) # 指定占位符宽度(左对齐) print("my name is {},my age is {}.".format("Nick", 18)) #一一对应传值 print("my name is {name},my age is {age}.".format(age=18, name="Nick")) #根据参数传值 name = "Nick" age = 18 print(f"His name is {name},my age is {age}.") #f-string print(F"my name is {name},my age is {age}.") #F大写F也可以