python⽇期格式打印_Python打印格式
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 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 values. Let’s see some examples here:
我们还可以格式化值。 让我们在这⾥看⼀些例⼦:
stark = {'first': 'Ned', 'second': 'Brandon', 'third': 'Rob'}
print('{first} {third}'.format(**stark))
The output will be:
输出将是:
⽇期时间格式 (Datetime Formatting)
We can also format 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格式化输出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:
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:
参考:
python ⽇期格式打印

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。