Python Print函数学习

来源:互联网 发布:ubuntu 16.10 安装后 编辑:程序博客网 时间:2024/06/01 08:02

参考链接:
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431643484137e38b44e5925440ec8b1e4c70f800b4e2000
http://www.tuicool.com/articles/eArqEfe

题目:利用print()输出1024*768=xxx`.

答案一:

代码
print('1024*768=',1024*768)
结果
1024*768= 786432
分析
可以发现在等号后多了一个空格,这样看起来非常不美观。为了让结果更加美观,有必要了解一下print()函数的原理。

print()函数
print()存在两个非常有用的内置方法sepend.
通过字面意思即可理解,sepseparation的简称,即代表分隔符.end则代表结束处的符号,在字符串结尾处追加一个值。通常情况下,sep默认是一个空格;end默认是一个换行符。

示例一
代码
print('FirstLine','SecondLine')
结果
FirstLine SecondLine

示例二
代码
print('FirstLine','SecondLine',sep='')
结果
FirstLineSecondLine

示例三
代码
print('FirstLine','SecondLine',end='_')
结果
FirstLine_SecondLine_

正确答案

代码
print('1024*768=',1024*768,seq='')
当然也可以使用字符串格式化形式来完成
print('1024*768={0}.format(1024*768))

0 0
原创粉丝点击