|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
课后题是:
请编写代码:使用 input() 函数让用户录入姓名,然后将名字保存到变量(name)中,最后使用 print() 函数打印出来。
答案如下:
>>> name = input("请输入您的名字:")
请输入您的名字:明珠
>>> print("你好", name, sep=",", end="!")
你好,明珠!
那位大神解释一下这句
print("你好", name, sep=",", end="!")
是怎么回事,怎么还有sep,end呢
本帖最后由 Peteryo01223 于 2021-5-21 16:52 编辑
- >>> help(print)
- Help on built-in function print in module builtins:
- 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.
复制代码
Python 里面有解释 print 这个函数,如上。这是 官方解释,最权威。
Print() 是个 BIF,内置函数,开发者提前写好的,方便我们大家直接调用。本质上,它也是一个函数啦,所以你看到的 sep 和 end,都是这个函数中,由开发者当时自己设定几个的参数,一般是隐藏的,看不到,但一直存在。其中:
- sep,即:seperation 分隔,默认值是空格。你可以试试看,给 sep 一个 *,你就能看到,str 之间,由* 分割开了。
- end,即:end 结尾,默认值是换一行。你试试,给 end 一个 \n\n\n,那么末尾就会回车三次,也就是换行三次。
这些参数都有默认值,平时你不写,python 就会按照默认值处理。
- >>> print('1','2','3')
- 1 2 3
- >>> print('1','2','3' , sep='*')
- 1*2*3
- >>> print('1','2','3' , sep='*', end = '\n\n\n')
- 1*2*3
- >>>
复制代码
|
|