python怎么输出结果_Python中print()常用输出方法

print函数用法

1.输出字符串

print 'Hello

World'

print "Hello

World"

print ('Hello

World')

print ("Hello

World")

[注] 对于Python 3.0及以上版本,print是函数,因此需要编写print

()而不是print。

输出结果:

Hello

World

Hello

World

Hello

World

Hello

World

2.

格式化输出整数

print 'The length

of \'%s\' is %d.' %('Hello World',len('Hello World'))

输出结果:

The length of

'Hello World' is 11.

3.

格式化输出十六进制、十进制、八进制整数

%x ——————

hex 十六进制

%d ——————

dec 十进制

%o ——————

oct 八进制

a =

0xFF

print

'hex=%x,dec=%d,oct=%o' %(a,a,a)

输出结果:

hex=ff,dec=255,oct=377

-----------------------------------------------------------------------

4.

格式化输出浮点数

%f ——————

输出浮点数

%.3f

—————— 保留三位小数输出浮点数

%6.3f

—————— 包含小数点共六位,小数点后保留三位输出浮点数

.3f ——————

包含小数点共六位,小数点后保留三位,不足补零输出浮点数

import

math

print 'PI=%f'

%math.pi

print 'PI=%.3f'

%math.pi

print 'PI=%6.3f'

%math.pi

print 'PI=%-6.3f'

%math.pi

print 'PI=.3f'

%math.pi

输出结果:

PI=3.141593

PI=3.142

PI=

3.142

PI=3.142

PI=03.142

5.

格式化输出字符串

%s ——————

输出字符串

print '%s'%'Today

is Tuesday'

输出结果:

Today is

Tuesday

6.

输出列表及字典

列表:

a=[1, 0xFA, 5,

'Python']

print

a

输出结果:

[1, 250, 5,

'Python']

输出字典:

a={'name':'Jack',

'gender':'Male', 'age':25, 'city':'London'}

print

a

输出结果:

{'gender':

'Male', 'age': 25, 'name': 'Jack', 'city': 'London'}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值