再谈 print
推荐一章关于 print 的文章,
无论学习java 语句 还是 python 我的第一行代码 用于 都是 hello word!,这个应该算得上是 程序界经典编程了吧。
按照书本内容,他讲述了python中的print 函数的一些新特性,如 print 同时打印 多个参数,参数与参数中间 用逗号,分割
name=\'张三\' sex=\'男\' age=18 print(name,sex,age) #运行结果 张三 男 18
在python这点还是比较可以的,因为在python 字符串相接遇到数字是必须用str()函数转一下(书本在这一章并没有提这个,我就突然想到的而已)。所以遇到数字和字符串输出到控制台就可以用这种方式,但是以后随着学习的越来越深入,肯定不是打印在控制台,而是输出到前端,或者其他地方,所以还是得用 str()函数,就像这样。
name=\'张三 \' sex=\'男 \' age=18 info=name+sex+str(age) print(info) #运行结果 张三 男 18
结合和上面的案例来看 print 多个参数打印,默认事空格(内容与内容之间空了一格),这是可以自定义的,如可以使用 - 来分割。就用 sep=\'-\'
name=\'张三\' sex=\'男\' age=18 print(name,sex,age,sep=\'-\') #运行结果 张三-男-18
其实print 很多参数 我们可以用 python 中的 help(类型) 函数(忘了前面有么有提到过,我也是看书和看视频学会的)
help(print) #运行结果 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,实际上书本并没有这些内容,因为我不想做一个只会敲书本案例的木偶人,学到这部分我联想到什么我就动手敲一下)。
-
- *args:复数,表示可以一次输出多个对象。输出多个对象时,需要用 , 分隔
- sep=\' \':用来间隔多个对象,默认值是一个空格。
- end=\'\n\': 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符串。
- file=None: 要写入的文件对象
这里讲一下 file 简单点说类似于日志,对输出内容进行持久化,因为类似于日志 所以他文件内容程累加状态。
#这是python 打开一个文件的方式 file=open(\'C:\\Users\\Administrator\\Desktop\\新建文本文档.txt\',\'a\') #使用字典 定义 一些列学生信息 student={\'1001\':{\'name\':\'张三\',\'age\':\'18\',\'sex\':\'男\'},\'1002\':{\'name\':\'李四\',\'age\':\'20\',\'sex\':\'男\'},\'1003\':{\'name\':\'村花\',\'age\':\'16\',\'sex\':\'女\'}} #定义表头 table ="学号 姓名 年龄 性别" #讲表头写入文件中 print(table,file=file) #格式化数据 #循环将 用户信息写入到 文件中 for id in student.keys(): stuinfo = \'{id:<} {name} {age:>1} {sex:>3}\' stus= student.get(id) stuinfo=stuinfo.format(id=id,name=stus.get(\'name\'),age=stus.get(\'age\'),sex=stus.get(\'sex\')); print(stuinfo,file=file)
运行两次后文件中的内容,写这个案例的时候,发现原来的很多东西都忘了,比如 字符串格式化 对齐(<>^)不过幸好我记得有这个东西。
今天也发现一个问题,原来格式化字符串中使用了{name} ,必须所有{name}都要赋值 。
info =\'{id:<} {name} {age:>1} {sex:>3}\' print(info.format(id=\'1001\')) #运行结果 Traceback (most recent call last): File "D:/develTools/PycharmProjects/project1/venv/Include/test3.py", line 3, in <module> print(info.format(id=\'1001\')) KeyError: \'name\'
解决办法就是 对每个{} 都进行赋值,其实最开始出现这个问题,我一脸蒙蔽,按常理来说,我不赋值应该按照字符串进行输出啊
info =\'{id:<} {name} {age:>1} {sex:>3}\' print(info.format(id=\'1001\',name=\'张三\',age=18,sex=\'男\')) #运行结果 1001 张三 18 男
其实只要调用了 format()函数 就必须 这样做,除非 不使用
info =\'{id:<} {name} {age:>1} {sex:>3}\' print(info) #运行结果 {id:<} {name} {age:>1} {sex:>3}
end 参数也很重要,因为 python 中的 print() 是必须换行的,那么因为 end 默认是 end=\'\n\' 若不想换行 可以指定为 end=‘’ 或者其他。
for i in range(10): print(i) #运行结果 这里是换行 0 1 2 3 4 5 6 7 8 9
不换行
for i in range(10): print(i,end=\'\') #运行结果 0123456789
觉得缩在一起不好看 那个指定一个分隔符 ,这里的分隔符 只是末尾加一个分隔符,如有多个参数打印,进行分割的话,还是需要指定 sep
for i in range(10): print(i,end=\'-\') #运行结果 0-1-2-3-4-5-6-7-8-9-
这里应该是 print 所有知识点了,以后要是遇到新的知识点,在进行补充说明。