yyz notes yyz notes
首页
  • RBAC权限设计
  • 架构图标设计
  • 账号体系
  • python基础
  • python高级
  • python模块
  • python设计模式
  • python数据结构与算法
  • django
  • django-DRF
  • flask
  • 直接设计开源pip包
  • 直接设计开源项目
  • python示例题/脚本
  • python面试题
  • golang基础
  • golang高级
  • golang常用组件
  • gin框架
  • es6
  • javascript
  • react
  • vue
  • TypeScript
  • mysql
  • redis
  • minio
  • elasticsearch
  • mongodb
  • 消息队列
  • 自动化测试
  • 操作系统

    • linux
    • windows
  • nginx
  • docker
  • k8s
  • git
  • ldap
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

益章

可乐鸡翅
首页
  • RBAC权限设计
  • 架构图标设计
  • 账号体系
  • python基础
  • python高级
  • python模块
  • python设计模式
  • python数据结构与算法
  • django
  • django-DRF
  • flask
  • 直接设计开源pip包
  • 直接设计开源项目
  • python示例题/脚本
  • python面试题
  • golang基础
  • golang高级
  • golang常用组件
  • gin框架
  • es6
  • javascript
  • react
  • vue
  • TypeScript
  • mysql
  • redis
  • minio
  • elasticsearch
  • mongodb
  • 消息队列
  • 自动化测试
  • 操作系统

    • linux
    • windows
  • nginx
  • docker
  • k8s
  • git
  • ldap
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • python基础

    • python概述

    • python环境搭建

    • python基础语法

      • 变量与常量
      • 基本数据类型-整数类型
      • 基本数据类型-浮点数类型
      • 基本数据类型-复数类型
      • 基本数据类型-布尔类型
      • 基本数据类型-bytes类型
      • 复杂数据类型-序列
      • 复杂数据类型-字符串
      • 复杂数据类型-列表
      • 复杂数据类型-元组
      • 复杂数据类型-字典
      • 复杂数据类型-集合
      • list,tuple,dict,set的区别和联系
      • 推导式
      • 浅拷贝和深拷贝
      • 运算符
      • 输入输出
        • 输入输出
        • 输入
        • 输出
          • *字符串格式化输出*
    • python关键字与标识符

    • python流程控制

    • python函数

    • python内置函数

    • python面向对象

    • python模块与包

    • python文件IO与OS

    • python异常处理机制

  • python高级

  • python模块

  • python设计模式

  • python数据结构与算法

  • django

  • django-DRF

  • flask

  • 自己设计开源pip包

  • 自己设计开源项目

  • python小示例

  • python面试题

  • python
  • python基础
  • python基础语法
YiZhang-You
2023-05-09
目录

输入输出

# 输入输出

# 输入

input()
1

# 输出

# 字符串格式化输出

字符串格式化是将各种值动态地放入字符串中。 可以使用%运算符或format()方法实现字符串格式化。

  • 格式化字符串字面值f

格式化字符串字面值 (opens new window)(简称为 f-字符串)在字符串前加前缀 f或 F,通过 {expression}表达式,把 Python 表达式的值添加到字符串内。

格式说明符是可选的,写在表达式后面,可以更好地控制格式化值的方式。下例将 pi 舍入到小数点后三位:

**import** **math>>>** print(f'The value of pi is approximately **{**math.pi**:**.3f**}**.')
The value of pi is approximately 3.142.
1
2

在 ':' 后传递整数,为该字段设置最小字符宽度,常用于列对齐:

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
...     print(f'{name:10} ==> {phone:10d}')
...
Sjoerd     ==>       4127
Jack       ==>       4098
Dcab       ==>       7678
1
2
3
4
5
6
7

还有一些修饰符可以在格式化前转换值。 '!a' 应用 [ascii()](https://docs.python.org/zh-cn/3/library/functions.html#ascii) ,'!s' 应用 [str()](https://docs.python.org/zh-cn/3/library/stdtypes.html#str),'!r' 应用 [repr()](https://docs.python.org/zh-cn/3/library/functions.html#repr):

>>> animals = 'eels'
>>> print(f'My hovercraft is full of {animals}.')
My hovercraft is full of eels.
>>> print(f'My hovercraft is full of {animals!r}.')
My hovercraft is full of 'eels'.
1
2
3
4
5
  • 字符串 format() 方法

[str.format()](https://docs.python.org/zh-cn/3/library/stdtypes.html#str.format) 方法的基本用法如下所示:

print('We are the **{}** who say "**{}**!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"
1
2

花括号及之内的字符(称为格式字段)被替换为传递给 [str.format()](https://docs.python.org/zh-cn/3/library/stdtypes.html#str.format) 方法的对象。花括号中的数字表示传递给 [str.format()](https://docs.python.org/zh-cn/3/library/stdtypes.html#str.format) 方法的对象所在的位置。

**>>>** print('**{0}** and **{1}**'.format('spam', 'eggs'))
spam and eggs
**>>>** print('**{1}** and **{0}**'.format('spam', 'eggs'))
eggs and spam
1
2
3
4

[str.format()](https://docs.python.org/zh-cn/3/library/stdtypes.html#str.format) 方法中使用关键字参数名引用值。

**>>>** print('This **{food}** is **{adjective}**.'.format(
**...**       food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.
1
2
3

位置参数和关键字参数可以任意组合:

**>>>** print('The story of **{0}**, **{1}**, and **{other}**.'.format('Bill', 'Manfred',
**...**                                                    other='Georg'))
The story of Bill, Manfred, and Georg.
1
2
3

如果不想分拆较长的格式字符串,最好按名称引用变量进行格式化,不要按位置。这项操作可以通过传递字典,并用方括号 '[]' 访问键来完成。

**>>>** table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
**>>>** print('Jack: **{0[Jack]:d}**; Sjoerd: **{0[Sjoerd]:d}**; '
**...**       'Dcab: **{0[Dcab]:d}**'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

**>>> for** x **in** range(1, 11):
**...**     print('**{0:2d}** **{1:3d}** **{2:4d}**'.format(x, x*x, x*x*x))
1
2
3
4
5
6
7
8
9
10
11
  • 手动格式化字符串

下面是使用手动格式化方式实现的同一个平方和立方的表:

>>> for x in range(1, 11):
...     print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
...     # Note use of 'end' on previous line
...     print(repr(x*x*x).rjust(4))
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 旧式字符串格式化方法
>>> import math
>>> print('The value of pi is approximately %5.3f.' % math.pi)
The value of pi is approximately 3.142.
1
2
3

开头的转换说明符对各种类型的数据进行格式化输出,具体请看下表。

转换说明符 解释
编辑 (opens new window)
上次更新: 2023/05/17, 23:08:21
运算符
标识符

← 运算符 标识符→

最近更新
01
配置yun源
05-24
02
linux-配置python虚拟环境
05-24
03
linux文件目录管理
05-24
更多文章>
Theme by Vdoing | Copyright © 2023-2023 yizhang | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式