python 日期格式打印_Python打印格式

本文详细介绍了Python的打印格式,包括打印多个值、分隔打印、格式值插值、价值对齐、签名数字、字典格式、日期时间格式和十进制格式等,提供了丰富的代码示例,帮助理解如何在Python中美化输出。
摘要由CSDN通过智能技术生成

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:

Python Print format multiple values

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

分隔打印中的多个值 (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:

Python Print formatting with Separator

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

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:

python print format interpolate

输出是干净的:

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:

python print formatting interpolate ordering

输出是相同的:

价值取向 (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:

python print formatting value center align

输出将是:

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:

python print format Signed Value Output

输出将是:

字典格式 (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:

Dictionary Formatting for python print

输出将是:

日期时间格式 (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:

python print datetime format

输出将是:

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:

python print decimal formatting

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 日期格式打印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值