python 日期格式打印

In this lesson, we will study about various ways for Python print format, through which we can print our data on the console and interpolate it.

在本课程中,我们将研究Python打印格式的各种方式,通过这些方式我们可以在控制台上打印数据并进行插值。

Python打印格式 (Python print format)

Easy print formatting sounds a small feature in any language but is one of the most used ones in daily programs. If a language supports easy print formatting, this will be a heaven for programmers! Let’s go on and study some examples for formatting.

简便的打印格式在任何语言中听起来都是一个小功能,但它是日常程序中使用最多的一种。 如果一种语言支持简单的打印格式设置,那么这将是程序员的天堂! 让我们继续研究一些格式化示例。

Please note that all the examples were tested on Python 3.6. Some of these might not work in Python 2.x as these were only introduced with Python 3.

请注意,所有示例均已在Python 3.6上进行了测试。 其中一些可能在Python 2.x中不起作用,因为这些仅在Python 3中引入。

Python打印格式示例 (Python print formatting examples)

Let’s start our journey with simple examples and building upon the slowly.

让我们以简单的示例开始我们的旅程,并逐步建立。

Python打印多个值 (Python printing multiple values)

A simple example will be to print multiple values. Let’s see how this is done:

一个简单的例子是打印多个值。 让我们看看如何做到这一点:

>>> a = 1
>>> b = 1.2
>>> c = "Python"
>>> print(a, b, c)

We will obtain the following result when we run this script:

运行此脚本时,将获得以下结果:

分隔打印中的多个值 (Separating multiple values in print)

In the last script, we saw how we can print multiple values. Here is a little modification to it:

在上一个脚本中,我们看到了如何打印多个值。 这是对它的一些修改:

>>> a = 1
>>> b = 1.2
>>> c = "Python"
>>> print(a, b, c, sep=",")

We will obtain the following result when we run this script:

运行此脚本时,将获得以下结果:

That looks cleaner, right?

看起来比较干净,对吧?

Python打印格式值插值 (Python print format value interpolation)

We can very easily interpolate values of any type in our print formats. Let’s see how this is done:

我们可以很容易地在我们的打印格式中插入任何类型的值。 让我们看看如何做到这一点:

name = "Shubham"
best_place = "JournalDev"
print("My name is {} and best place to study programming is {}".format(name, best_place))

The output is clean:

输出是干净的:

There are more ways to do this. Here is a little modification to the script:

有更多方法可以做到这一点。 这是对该脚本的一些修改:

name = "Shubham"
best_place = "JournalDev"
print("My name is {0} and best place to study programming is {1}".format(name, best_place))

The print format output will remain same like the last program.

打印格式输出将与上一个程序相同。

Actually, we can even modify the order of the values in format tuple as:

实际上,我们甚至可以将元组格式的值的顺序修改为:

name = "Shubham"
best_place = "JournalDev"
print("Best place to study programming is {1}, my name is {0}".format(name, best_place))

The output is the same:

输出是相同的:

价值取向 (Value Alignment)

We can even apply alignment to our outputs. Let’s see some examples here to center align our output:

我们甚至可以将对齐方式应用于我们的输出。 让我们来看一些示例,以使输出居中对齐:

name = '{:^20}'.format('journaldev')
print(name)

The output will be:

输出将是:

If you run the program yourself, you will notice that as the string journaldev is actually 10 characters long, there 5 gaps before and 5 gaps after the String. Also, 20 decides the total length of the output including the String.

如果您自己运行程序,您会注意到由于字符串journaldev实际上是10个字符长,因此在字符串之前有5个journaldev ,在字符串journaldev有5个journaldev 。 同样, 20确定包括字符串在内的输出的总长度。

签名号码 (Signed Numbers)

We can also print numbers with a signed value. Let’s see some examples here:

我们还可以打印带有符号值的数字。 让我们在这里看一些例子:

print('{:+d}'.format(42))

Output will be:

输出将是:

字典格式 (Dictionary Formatting)

We can also format python dictionary values. Let’s see some examples here:

我们还可以格式化python字典值。 让我们在这里看一些例子:

stark = {'first': 'Ned', 'second': 'Brandon', 'third': 'Rob'}
print('{first} {third}'.format(**stark))

The output will be:

输出将是:

日期时间格式 (Datetime Formatting)

We can also format Datetime values. Let’s see some code snippets here:

我们还可以格式化日期时间值。 让我们在这里看到一些代码片段:

from datetime import datetime
print('{:%Y-%m-%d %H:%M}'.format(datetime(2017, 12, 7, 13, 22)))

The output will be:

输出将是:

This allows us to format our DateTime values inline. This formatting option wasn’t available prior Python 2.6.

这使我们可以内联格式化DateTime值。 Python 2.6之前没有此格式选项。

We can try another formatting option with DateTime with which we can provide separate options for date and time:

我们可以尝试使用DateTime的另一个格式化选项,通过它我们可以为日期和时间提供单独的选项:

from datetime import datetime
myDate = datetime(2017, 12, 7, 13, 22)
print('{:{dfmt} {tfmt}}'.format(myDate, dfmt='%Y-%m-%d', tfmt='%H:%M'))

The output will be the same as in previous image.

输出将与先前的图像相同。

十进制格式 (Decimal Formatting)

We can also format Decimal values up to a precision point. Let’s see some code snippets here:

我们还可以将十进制值格式化为一个精度点。 让我们在这里看到一些代码片段:

value = '{:{width}.{prec}f}'.format(3.1428, width=5, prec=2)
print(value)

The output will be:

We can try even without providing a width of course when we’re unsure of the answer.

输出将是:

如果我们不确定答案,我们甚至可以尝试不提供任何宽度。

Python打印格式摘要 (Python print formatting summary)

In this lesson on print formatting in Python, we saw how we can format our values in common ways. Use them to beautify the output.

在有关使用Python打印格式的本课中,我们了解了如何以通用方式格式化值。 使用它们可以美化输出。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/17370/python-print-format

python 日期格式打印

python 日期格式打印_Python打印格式相关推荐

  1. python字符串转浮点数_Python | 打印不同的值(整数,浮点数,字符串,布尔值)...

    python字符串转浮点数 In the given example, we are printing different values like integer, float, string and ...

  2. python日期和时间_Python日期和时间

    datetime是Python处理日期和时间的标准库. 获取当前日期和时间 我们先看如何获取当前日期和时间: 1 2 3 4 5 6>>>from datetime import d ...

  3. python调用所有函数_python打印所有函数调用以了解脚本

    这是名为player.py的代码.在# player.py def foo(): pass def bar(): foo() def car(): bar() pass car() 像$python ...

  4. python水仙花数总结_python打印n位数“水仙花数”(实例代码)

    注:所谓n位数"水仙花数"是指一个n数,其各位数字n次方和等于该数本身.如三位数"水仙花数"是指一个三位数,其各位数3次方和等于该数本身. 一.3位数" ...

  5. python中调用万年历_python 打印万年历

    题目:打印万年历 已知条件 闰年条件:能被4整除且不能被100整除,或者能被400整除 1900年1月1日 是周一 解题思路 判断闰年; 判断当月有多少天; 这个月的1号是从周几开始的; 格式化打印日 ...

  6. python直角三角形型编程_python打印直角三角形与等腰三角形实例代码

    前言 本文通过示例给大家详细介绍了关于python打印三角形的相关,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 1.直角三角形 #i控制行数j控制*的个数 for i in ran ...

  7. python类的属性打印_python 打印类的属性、方法

    打印变量db的类(class): [root@fuel ~]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 2 ...

  8. python九九乘法口诀_Python打印出九九乘法口诀

    前几天看到一个Python交流群里的一个朋友问问题,在Python里如何打印出九九乘法口诀,要阶梯的形式显示出来,我突然想起了多年以前刚刚开始学习Delphi的时候也做过类似的题目,如今开始玩Pyth ...

  9. python输出星号等腰三角形_python打印直角三角形与等腰三角形实例代码

    python打印直角三角形与等腰三角形实例代码 前言 本文通过示例给大家详细介绍了关于python打印三角形的相关,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 1.直角三角形 #i ...

最新文章

  1. Ubantu Mark
  2. 超越“机器人三定律” 人工智能期待新伦理
  3. ExtJs学习笔记(4)_EditorGridPanel(可编辑的网格控件)
  4. 辽宁师范大学java_辽宁师范大学心理学院
  5. Linux登陆Mariadb数据库,Mariadb数据库的远程连接(centos 7+ Navicat)
  6. 【渝粤教育】电大中专市场营销管理20作业 题库
  7. python数据结构的应用场景不包括,Python 数据结构学习
  8. 网站维护页面_营销型企业网站有哪些功能?
  9. retinex 的水下图像增强算法_图像增强论文:腾讯优图CVPR2019
  10. 使用cdn和npm引入的区别_中央空调和新风系统有什么区别?有必要一起装吗?
  11. InstanceBeginEditable dw中特有标识
  12. 【爬虫】花瓣采集下载器
  13. 寨板p45黑苹果10.12.6
  14. Qt界面程序中嵌入其他可执行exe程序
  15. 金网奖首度跨界心理学,打造最强案例
  16. destoon php版本,Destoon 7.0最新版常见问题解决方法
  17. 一文彻底学会Redis主从复制(高可用)
  18. DQN:Playing Atari with Deep Reinfocement Learning
  19. 在腾讯云部署一个自己的网站 问题总结
  20. pdf转换成excel转换器

热门文章

  1. Java基础知识强化87:BigInteger类之BigInteger加减乘除法的使用
  2. Unity的状态机设计
  3. 工程linux下创建svn仓库目录结构
  4. [转载] 生成对角矩阵 numpy.diag
  5. [转载] 消息中间件学习总结(8)——RocketMQ之RocketMQ捐赠给Apache那些鲜为人知的故事
  6. Spring的数据库编程浅入浅出——不吹牛逼不装逼
  7. ESP分区重建,解决各种引导问题
  8. 数据库操作--批量修改数据库表名及字段名大小写转换及首字母大写+删除没有数据无用的表...
  9. [翻译]NUnit---Explicit and Ignore Attributes(十二)
  10. scrum 11.27