6、Python 输入输出

一 、输入

1、说明

输入输出,简单来说就是从标准输入中获取数据和将数据打印到标准输出,常被用于交互式的环境当中,Python中 input()来输入标准数据

2、语法格式

格式:input()

功能:接受一个标准输入数据,

返回:返回string类型。ctrl+z结束输入

3、示例代码

  1. 等待一个任意字符的输入
    input('请输入用户名:\n')
    
  2. 接受多个数据输入,使用eval()函数,间隔符必须是逗号
    a,b,c=eval(input())
    

二、输出

1、说明

Python一共有两种格式化输出语法。

一种是类似于C语言printf的方式,称为 Formatting Expression

一种是类似于C#的方式,称为String Formatting Method Calls

2、格式化输出

1、整数的输出
  1. 语法说明
    格式化符号格式说明备注
    %o八进制oct
    %d十进制dec
    %x十六进制hex
  2. 举个栗子
    print('%o' % 20) # 八进制
    24
    print('%d' % 20) # 十进制
    20
    print('%x' % 24) # 十六进制
    18
    
2、浮点数输出
  1. 语法说明
    格式化符号说明备注
    %f保留小数点后面六位有效数字
    %e保留小数点后面六位有效数字
    %g在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
  2. 举个栗子
    print('%f' % 1.11)  # 默认保留6位小数
    1.110000
    print('%.1f' % 1.11)  # 取1位小数
    1.1
    print('%e' % 1.11)  # 默认6位小数,用科学计数法
    1.110000e+00
    print('%.3e' % 1.11)  # 取3位小数,用科学计数法
    1.110e+00
    print('%g' % 1111.1111)  # 默认6位有效数字
    1111.11
    print('%.7g' % 1111.1111)  # 取7位有效数字
    1111.111
    print('%.2g' % 1111.1111)  # 取2位有效数字,自动转换为科学计数法
    1.1e+03
    
3、字符串输出
  1. 语法说明
    格式化符号说明备注
    %s字符串输出string
    %10s右对齐,占位符10位
    %-10s左对齐,占位符10位
    %.2s截取2位字符串
    %10.2s10位占位符,截取两位字符串
  2. 举个栗子
    print('%s' % 'hello world')  # 字符串输出
    hello world
    print('%20s' % 'hello world')  # 右对齐,取20位,不够则补位
             hello world
    print('%-20s' % 'hello world')  # 左对齐,取20位,不够则补位
    hello world         
    print('%.2s' % 'hello world')  # 取2位
    he
    print('%10.2s' % 'hello world')  # 右对齐,取2位
            he
    print('%-10.2s' % 'hello world')  # 左对齐,取2位
    he
    

3、Formatting方法

相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’

1、基本用法
  1. 不带编号,即“{}”
    print('{} {}'.format('hello','world'))  # 不带字段
    hello world
    
  2. 带数字编号,可调换顺序,如: “{1}”、“{2}”
    print('{0} {1}'.format('hello','world'))  # 带数字编号
    hello world
    print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序
    hello world hello
    print('{1} {1} {0}'.format('hello','world'))
    world world hello
    
  3. 带参数,即“{a}”、“{b}”
    print('{a} {b} {c}'.format(tom='hello',a='world' ,c='python'))  # 带参数
    world hello 
    
2、进阶用法
  1. <(默认)左对齐、> 右对齐、^中间对齐、=(只用于数字)在小数点后进行补齐
    print('{}{}'.format('hello','world'))  # 默认左对齐
    helloworld
    print('{:10s} and {:>10s}'.format('hello','world'))  # 取10位左对齐,取10位右对齐
    hello      and      world
    print('{:^10s} and {:^10s}'.format('hello','world'))  # 取10位中间对齐
      hello    and   world   
    
  2. 取位数“{:4s}”、"{:.2f}"等
    print('{} is {:.2f}'.format(1.123,1.123))  # 取2位小数
    1.123 is 1.12
    print('{0} is {0:>10.2f}'.format(1.123))  # 取2位小数,右对齐,取10位
    1.123 is       1.12
    

4、其它

1、自动换行
print (1)
print (2)
2、不换行
for i in range(0,3):
     print(i, end = '' )
012

5、format与%方式的优点

  1. 不需要理会数据类型 (python3以上的版本都是可以用%s)
  2. 单个参数可以多次输出,参数顺序可以不同
  3. 填充方式十分灵活,对齐方式异常强大
  4. 官方推荐用的方式,%方式在后面的版本终将会被淘汰
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值