打印下面字符串的3种方式
给变量 赋值:
name = "Zhengzi" age = 26
1.连接符+拼接
print("name is : " + name + ",age is :" + str(age)) # + 连接字符串,age为整型,不能直接连接,需要转换
2.格式符替换
print("name is: %s ,age is: %d" % (name, age)) # %s,%d分别替换指定字符串,指定数字,不确定打印类型用%r替换
3.格式化format()
print("name is :{}, age is :{}".format(name, age)) # format()格式化,默认顺序 # 用{0},{1}指定位置 print("name is :{1}, age is :{0}".format(age, name)) # 用变量指定对应的关系 print("name is :{n}, age is :{a}".format(a=age, n=name))