python输入与输出

一、输出

s = 'Hello\n'
# str()函数返回一个用户易读的表达形式
print(str(s))  # Hello
# repr()产生一个解释器易读的表达形式,可以转义字符串中的特殊字符
print(repr(s))  # 'Hello\n'
# str.format() 函数来格式化输出值
print('Hello {}{point}'.format('Berry', point='!'))  # Hello Berry!
print('{1} {0}'.format('Hello', 'Berry'))  # Berry Hello
# 输出一个表
for x in range(1, 6):
    # rjust()方法可以将字符串靠右, 并在左边填充空格
    print(repr(x).rjust(3), repr(x**2).rjust(3), repr(x**3).rjust(3))
for x in range(1, 6):
    # 可选项 : 和格式标识符可以跟着字段名
    # 在 : 后传入一个整数, 可以保证该域至少有这么多的宽度
    print('{0:3d} {1:3d} {2:3d}'.format(x, x**2, x**3))
#   1   1   1
#   2   4   8
#   3   9  27
#   4  16  64
#   5  25 125
# zfill()会在数字的左边填充0
print('ba'.zfill(5))  # 000ba
table = {'one': 1, 'two': 2, 'three': 3}
print('three:{0[three]:d}, two:{0[two]:d}, one:{0[one]:d}'.format(table))
# **参数收集所有未匹配的关键字参数组成一个dict对象
print('three:{three:d}, two:{two:d}, one:{one:d}'.format(**table))
# three:3, two:2, one:1
# 旧的字符串格式化
print('hello %5s' % 'Berry')  # hello Berry

二、输入

print("what's your name?")
name = input()
print('I am', name, '!')
# what's your name?
# IronMan
# I am IronMan !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值