字符串格式化输出
%s是全能占位符,可以为任何类型占位 ,比如int、string、bool
%d是数字占位符只能整型占位
两种方式:(用print方法举例)
1) print("字符 %s,字符%s" % (变量,变量))
2)print(f"字符{变量},字符{变量}") 这种方式是在Python3.5之后的版本中可以用
第一种代码示例:
1
2
3
4
5
|
name = input ( "输入你的名字:" ) adress = input ( "你来自哪里:" ) wife = input ( "你妻子是:" ) unlike = input ( "你不喜欢的明星" ) print ( "我是%s,来自%s,妻子是%s,不喜欢%s" % (name,adress,wife,unlike)) |
第二种代码示例:
1
2
3
4
5
|
name = input ( '你的名字:' ) adress = input ( "你来则哪里:" ) wife = input ( "你妻子是:" ) unlike = input ( '你不喜欢的明星是' ) print (f "你叫{name},你来自{adress},你妻子是{wife},你不喜欢{unlike}" ) |