基础用法
print语句可以向屏幕上输出指定的文字
例如:
print 'Hello World!'
print语句也可以跟上多个字符串,用逗号“,”隔开,就可以连成一串输出
ps:print会依次打印每个字符串,遇到逗号“,”会输出一个空格
例如:
print 'my','name','is','Jacky'
输出为:
my name is Jacky
print语句也可以跟上多个字符串,若无”,”,或者手动空格,都是无法在拼接时显示空格的
print 'my''name''is''Jacky'
print 'my' 'name' 'is' 'Jacky'
输出为:
mynameisTom
print也可以用来打印数值或者是计算结果
print 100
print 100 * 300
print中字符串和数字之间需要用”,”来连接。
ps:如果不用”,”来连接,则会报错SyntaxError: invalid syntax
print 'hello:', 100
print "三位数依次为:", numList
格式化输出
格式化输出是指通过print等函数向指定的地方(例如屏幕,文件)输出指定格式的内容.
例如:%d 输出整数,%s 输出字符串。 %s、%d、%f是占位符
打印字符串
以下三种方式都可以输出格式化的字符串
print ("His name is %s"%("www"))
print "His name is %s" % ("jacky")
print "His name is %s" % "who"
输出为:
His name is www
His name is jacky
His name is who
打印整数
%d –只能对应int类
print ("He is %d years old") % (25)
输出为:
He is 25 years old
a = 3.1415926
print "%d" %a
输出为:
3
打印浮点数
a = 3.1415926
print "%f" %a
输出为:
3.141593
1
2
3
4
打印浮点数(指定保留小数点位数)
a = 3.1415926
print "%.2f" %a #按照要求输出小数位数
输出为:
3.14
print "%.9f" %a #如果要求的小数位数过多,后面就用0补全
输出为:
3.141592600
Reference
FEATURED TAGS
algorithm
algorithmproblem
architecturalpattern
architecture
aws
c#
cachesystem
codis
compile
concurrentcontrol
database
dataformat
datastructure
debug
design
designpattern
distributedsystem
django
docker
domain
engineering
freebsd
git
golang
grafana
hackintosh
hadoop
hardware
hexo
http
hugo
ios
iot
java
javaee
javascript
kafka
kubernetes
linux
linuxcommand
linuxio
lock
macos
markdown
microservices
mysql
nas
network
networkprogramming
nginx
node.js
npm
oop
openwrt
operatingsystem
padavan
performance
programming
prometheus
protobuf
python
redis
router
security
shell
software testing
spring
sql
systemdesign
truenas
ubuntu
vmware
vpn
windows
wmware
wordpress
xml
zookeeper