输入input 输出print 函数
print内置函数
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
注意:print(value,...,sep='',end='\n',file=sys.stdout,flush=False)
print内置函数,print('内容1','内容2',...,sep='用来隔开输出内容的默认字符',end='行尾输出内容,可以更改', file.....,flush一般使用false)
input内置函数
def input(*args, **kwargs): # real signature unknown
"""
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
"""
注意: 不管用户输入的是什么,变量保存的结果都是字符串。可以通过内置type()函数来确定一个变量的类型,一个input接收一次用户的输入,如果想要接收多次用户的输入,使用多个input
%和{}占位符
可以使用%占位符来表示格式化一个字符串,如%d %s %f等和C一样,name ='zhangsan' age=18 print('大家好,我的名字是%s,我今年%d岁了' %(name, age)) #结果是"大家好,我的名字是zhangsan,我今年18岁了" 和print('大家好,我的名字是',name , '我今年',age,‘岁了’,sep=' ' )
%s ==> 表示的是字符串的占位符
%d ==> 表示的是整数的占位符
%nd ==> 打印时,显示n位,如果不够,在前面使用空格补齐
%-nd ==> 打印时,显示n位,如果不够,在后面使用空格补齐
%f ==> 表示的是浮点数的占位符
%.nf ==> 保留小数点后n 位
%% 两个%就把%变成普通的一个%字符,没有转义的作用了
{}也可以进行占位,什么都不屑,会读取后面的内容,一一对应填充
-eg:x='大家好,我是{},我今年{}岁了'.format('张三',18)
当然也可以指定{数字}里的数字进行指定是顺序来对应,根据数字的顺序来进行填入,数字从0开始
-eg:y='大家好,我是{1},我今年{0}岁了'.format(20,'jerry')
还可以 ’我是{name},我今年{age}岁了,我来自{address}‘.format(age=18,name='zhangsan',address='hubei')
上面的两种可以混合使用,但是需要注意数字的要放在前面,变量要放在后面,{}里什么都不写不能和数字一起混合使用
d=['zhangsan',18,'上海',180]
b='大家好,我是{},我今年{}岁了,我来自{},身高{}cm'.format(*d)
print(b)
info={'name':'chris','age':23,'addr':'北京','height':190}
c='大家好,我是{name},我来自{addr},身高{height}cm,我今年{age}岁了'.format(**info)
print(c)
生命依靠吸收负熵,避免了趋向平衡的衰退

浙公网安备 33010602011771号