0%

6、输入输出.md

Python-控制台输入输出

一 、输入

1、说明

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

2、语法格式

格式:input()

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

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

3、示例代码

  1. 等待一个任意字符的输入

    1
    input('请输入用户名:\n')

  2. 接受多个数据输入,使用eval()函数,间隔符必须是逗号

    1
    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. 举个栗子

    1
    2
    3
    4
    5
    6
    print('%o' % 20) # 八进制
    24
    print('%d' % 20) # 十进制
    20
    print('%x' % 24) # 十六进制
    18

2、浮点数输出

  1. 语法说明

    格式化符号 说明 备注
    %f 保留小数点后面六位有效数字
    %e 保留小数点后面六位有效数字
    %g 在保证六位有效数字的前提下,使用小数方式,否则使用科学计数法
  2. 举个栗子

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    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.2s 10位占位符,截取两位字符串
  2. 举个栗子

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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. 不带编号,即“{}”

    1
    2
    print('{} {}'.format('hello','world'))  # 不带字段
    hello world
  2. 带数字编号,可调换顺序,如: “{1}”、“{2}”

    1
    2
    3
    4
    5
    6
    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}”

    1
    2
    print('{a} {b} {c}'.format(tom='hello',a='world' ,c='python'))  # 带参数
    world hello

2、进阶用法

  1. < (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐

    1
    2
    3
    4
    5
    6
    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}”等

    1
    2
    3
    4
    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、自动换行

1
2
print (1)
print (2)

2、不换行

1
2
3
for i in range(0,3):
print(i, end = '' )
012

5、format与%方式的优点

  1. 不需要理会数据类型 (python3以上的版本都是可以用%s)
  2. 单个参数可以多次输出,参数顺序可以不同
  3. 填充方式十分灵活,对齐方式异常强大
  4. 官方推荐用的方式,%方式在后面的版本终将会被淘汰