格式化字符串的重点

用户交互Input函数

  1. input()函数输入的东西被封装成字符串输出
  2. python2中的输入需要指定输入的函数类型
  3. python2中的rew_input()和input()是一样的
s = input("输入:")
print(s)
print(type(s))

基本运算符

  1. 算数运算符
  • // 表示取整除(地板除) %取余除(只保留余数)
  1. 比较运算符
  • 比较的一般是内存地址,= 表示赋值 “==”表示比较
  1. 赋值运算符
  • 例如 a **=b 等同于a = a ** b
  1. 身份运算符
  • is (not is)
  1. python运算符优先级
  1. 链式赋值
  • a = b = c = 10
  1. 交叉赋值
  • a, b = b, a
  1. 解压缩
  • 对列表中的值多次取出
name_list = ['nick', 'egon', 'jason']
x = name_list[0]
y = name_list[1]
z = name_list[2]
print(f'x:{x}, y:{y}, z:{z}')

格式化输出

  1. 占位符
    用%s先占取待定的位置,后面分别按照顺序取出占位的值
  2. format格式化
    和占位符原理相似,在表达式内用{}占位,最后用.format()统一指向站位的地方
  3. f-string格式化
    直接在表达式前使用f表示该表达式是格式化输出,句中用{}来占位

三种格式化字符串方式演示

name = 'shuai_bi'
height = 189
weight = 130

print(f"my name is {name} ,my height is {height}, my weight is {weight} ")
print("my name is %s my weight is %d my height is %d" % (name,weight,height))
print("my name is {},my height is {},my weight is {}".format(name, height, weight))
原文地址:https://www.cnblogs.com/Dr-wei/p/10901699.html