Python入门——Python基础(三)

本文深入讲解Python中的基本输入输出操作,包括input函数的使用方法及其参数解析,以及print函数的功能和格式设定,如sep和end参数的作用。适合Python初学者快速掌握核心I/O技能。

基本输入输出

input

python官方文档对input函数的说明
input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.
注意:prompt参数可以为空

用Python进行程序设计,输入是通过input( )函数来实现的
x = input(‘提示:’)该函数返回输入的对象。可输入数字、字符串和其它任意类型对象。
在Python 3.x中,input()函数用来接收用户的键盘输入,不论用户输入数据时使用什么界定符,input()函数的返回结果都是字符串,需要将其转换为相应的类型再处理。

# input实例
s = input('--> ')  
--> Monty Python's Flying Circus
s  
"Monty Python's Flying Circus"

print

基本输出函数 print
作用:

将一系列的值以字符串形式输出到标准输出设备上,默认为终端
格式
print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
官方解释

Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(…) instead.
Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

在python初级学习阶段
print函数只需要掌握这种语法格式:print(*objects, ········,sep=’ ‘, end=’\n’)
print函数里面的关键字参数为:

sep 两个值之间的分隔符 默认为一个空格
end 输出完毕后在流末尾自动追加一个字符,默认为换行符“\n”

print(0, 1, 2, 3, 4, 5)      ------> 0 1 2 3 4 5
print(0, 1, 0, 2, 0, 3,sep="! ")    ------>  0! 1! 0! 2! 0! 3
print(0, 0, 1, 1, 2, 2,sep = "% ",end = "\n\n\n\n\n\n" ) ----->  0% 0% 1% 1% 2% 2  (\n 换行符,表示换行 即  回车)






print(0, 1, 0, 2, 0, 3,end = " ")------>  end = ""    表示不换行 
print("sss")        --------->     0 1 0 2 0 3 sss
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

壮壮Loschicos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值