python format 格式化 输出

简介: python format 格式化 输出

从格式化表达式到方法

format:格式化方法。因为它知识上是使用了str的__format__方法。

基本的操作

所谓格式化方法,就是可以先建立一个输出字符串的模板,然后用format来填充模板的内容。

>>> #先做一个字符串模板
>>> template = "My name is {0}. My website is {1}. I am writing {2}."

>>> #用format依次对应模板中的序号内容
>>> template.format("hiekay","hiekay.github.io","python")
'My name is hiekay. My website is hiekay.github.io. I am writing python.'

当然,上面的操作如果你要这样做,也是可以的:

>>> "My name is {0}. My website is {1}. I am writing {2}.".format("hiekay","hiekay.github.io","python")
'My name is hiekay. My website is hiekay.github.io. I am writing python.'

除了可以按照对应顺序(类似占位符了)填充模板中的位置之外,还可以用关键字来指明所应该填写的内容。

>>> template = "My name is {name}. My website is {site}"
>>> template.format(site='hiekay.github.io', name='hiekay')
'My name is hiekay. My website is hiekay.github.io'

关键词所指定的内容,也不一定非是str,其它的数据类型也可以。此外,关键词和前面的位置编号,还可以混用。比如:

>>> "{number} is in {all}. {0} are my number.".format("seven",number=7,all=[1,2,3,4,5,6,7,8,9,0])
'7 is in [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]. seven are my number.'

看输出结果,就知道,经过format方法得到是一个新的str。

序列对象的偏移量

有这样一个要求:在输出中,显示出一个单词的第一个字母和第三个个字母。比如单词python,要告诉看官,第一字母是p,第三个字母是t。

这个问题并不难。实现方法也不少,这里主要是要展示一下偏移量在format中的应用。

>>> template = "First={0[0]}, Third={0[2]}"
>>> template.format(word)
'First=p, Third=t'

list也是序列类型的,其偏移量也可。

>>> word_lst = list(word)
>>> word_lst
['p', 'y', 't', 'h', 'o', 'n']
>>> template
'First={0[0]}, Third={0[2]}'
>>> template.format(word_lst)
'First=p, Third=t'

对上面的综合一下,稍微啰嗦一点的实验:

>>> template = "The word is {0}, Its first is {0[0]}. Another word is {1}, Its second is {1[1]}."
>>> template.format("python","learn")
'The word is python, Its first is p. Another word is learn, Its second is e.'

>>> "{name}\'s first is {name[0]}".format(name="hiekay")    #指定关键词的值的偏移量
"hiekay first is h"

值得注意的是,偏移量在序列类型的数据中,因为可以是负数,即能够从右边开始计数。

>>> word
'python'
>>> word[-1]
'n'
>>> word[-2]
'o'

但是,在模板中,无法使用负数的偏移量。

>>> "First={0[0]}, End={0[-1]}".format(word) #报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string indices must be integers, not str

>>> "First={0[0]}, End={0[5]}".format(word)  #把-1改为5就可以了。
'First=p, End=n'

当然,放到模板外面是完全可行的。这样就好了:

>>> "First={0}, End={1}".format(word[0],word[-1])
'First=p, End=n'

dictionary的键

直接上实验:

>>> myinfo
{'website': 'hiekay.github.io', 'name': 'hiekay', 'room': 703}
>>> template = "I am {0[name]}"
>>> template.format(myinfo)
'I am hiekay'
>>> template = "I am {0[name]}. My QQ is {qq}"
>>> template.format(myinfo,qq="123456")
'I am hiekay. My QQ is 123456'

位置后面跟键,就能得到format的参数中字典的键对应的值。除了根据位置得到,还能够根据关键词得到:

>>> myinfo
{'website': 'hiekay.github.io', 'name': 'hiekay', 'room': 703}
>>> "my website is {info[website]}, and I like {0}".format("python",info=myinfo)    #关键词info引用的是一个字典
'my website is hiekay.github.io, and I like python'

模板中添加属性

实验:

>>> import math
>>> "PI is {PI.pi}".format(PI=math)
'PI is 3.14159265359'

这是用关键词,下面换个稍微复杂点,用位置的。

>>> import sys,math
>>> 'PI is {0.pi}. My lptop runs {1.platform}'.format(math,sys)
'PI is 3.14159265359\. My lptop runs linux2' 

其它进制

在这个世界上的数学领域,除了有我们常常用到的十进制、十二进制(几点了,这是你我常用到的,钟表面就是12进制)、六十进制(这个你也熟悉的)外,还有别的进制,比如二进制、八进制、十六进制等等。进制的确在计算机最底层是用二进制的。

  • 输出时候的进制问题。
>>> "{0:X}, {1:o}, {2:b}".format(255,255,255)
'FF, 377, 11111111'
  • X:十六进制,Hex
  • o:八进制,octal
  • b:二进制,binary

顺便补充,对于数的格式化方法输出和格式化表达式一样,就不赘述了。

目录
相关文章
|
11天前
|
Python
【10月更文挑战第6天】「Mac上学Python 12」基础篇6 - 输入输出与格式化详解
本篇将详细介绍Python中的输入和输出函数,包括 `print()` 和 `input()` 函数的使用,涵盖格式化输出、类型转换及常见的字符串格式化方法。通过学习本篇,用户将掌握如何使用Python进行输入输出操作,并能灵活运用格式化输出处理数据。
53 1
【10月更文挑战第6天】「Mac上学Python 12」基础篇6 - 输入输出与格式化详解
|
19天前
|
Python
Python 格式化输出的高级技巧与应用
Python 格式化输出技术可让数据展示更加清晰易读。本文介绍了四种高级技巧:使用 `%` 占位符进行简单格式化;利用 `format()` 方法提供更灵活的参数位置控制;通过 `{:.2f}` 格式化浮点数保留两位小数;使用 `&lt;`, `&gt;`, `^` 对齐方式及字符填充;最后,展示了如何用特定格式代码格式化日期和时间。这些技巧能够显著提升代码的可读性和美观性。
21 6
|
18天前
|
Python
python时间格式化/时间格式转换
python时间格式化/时间格式转换
21 0
|
4月前
|
Python
通过f-string编写简洁高效的Python格式化输出代码
Python 3.6中引入的f-string是Python中最常用的特征之一,它可以让我们编写更干净、更高效和更易于维护的代码,我们今天就由浅入深来详细介绍使用它的一些技巧。
456 4
|
4月前
|
Python
Python语言提供了多种输出格式化的方法,这些方法随着时间的推移和版本的更新而发展
【6月更文挑战第19天】Python格式化方法包括过时的`%`操作符,`str.format()`,推荐的f-string(Python 3.6+)和Template strings。f-string提供最佳的可读性和性能,`str.format()`是通用的,而`%`不推荐使用。模板字符串用于特定场景。对于旧版Python,使用`str.format()`或`%`。
46 4
|
4月前
|
IDE 前端开发 开发工具
怎么在isort Python 代码中的导入语句进行排序和格式化
`isort` 是一个Python工具,用于自动排序和格式化代码中的导入语句,提高代码整洁度和可读性。它支持自动排序、保留空白和注释、自定义排序规则、与多种编辑器集成以及命令行使用。安装`isort`可通过`pip install isort`,使用时可直接在Python代码中导入或通过命令行处理文件。示例展示了如何在代码中使用`isort`进行导入排序,包括基本排序、自定义设置和处理多个文件。`isort`适用于标准库、第三方库和自定义模块的导入排序,还可忽略特定导入,并能与IDE和编辑器插件集成,提升开发效率。
|
4月前
|
IDE 开发工具 开发者
isort——Python 代码中的导入语句进行排序和格式化
isort,全称是 "Import Sorting",是一个 Python 工具,用来对 Python 代码中的导入语句进行排序和格式化。它可以帮助我们按照一定的规则对导入的模块进行排序,使得代码更加整洁,易于阅读和维护。
|
4月前
|
IDE 开发工具 Python
black--一键格式化Python代码
black--一键格式化Python代码
|
4月前
|
Python
Python 字符串格式化的方式有哪些?
这篇文章主要介绍了Python的字符串格式化方法,包括: 1. `%` 操作符,如 `%s`, `%d`, `%f` 用于基本的变量插入和类型转换。 2. `str.format()` 方法,利用 `{}` 占位符和位置或关键字参数。 3. f-strings (Python 3.6+),直接在字符串前加 `f` 并在花括号内嵌入变量。 4. `string.Template` 模块,使用 `$` 符号进行模板替换。 5. `str.format_map()` 方法,接受字典替换占位符。 文章强调f-strings在新代码中的推荐使用,因其简洁和可读性。