2024-05-15 01:59:46 发布
网友
我需要知道如何将数字格式化为字符串。我的代码在这里:
return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm
小时和分钟是整数,秒是浮点数。函数的作用是:将所有这些数字转换成十分位(0.1)。因此,它将显示类似“5.0:30.0:59.1 pm”的内容,而不是我的字符串输出“5:30:59.07 pm”。
底线是,我需要什么库/函数来为我这样做?
从Python2.6开始,有一个替代方法:方法str.format()。下面是一些使用现有字符串格式运算符(%)的示例:
str.format()
%
>>> "Name: %s, age: %d" % ('John', 35) 'Name: John, age: 35' >>> i = 45 >>> 'dec: %d/oct: %#o/hex: %#X' % (i, i, i) 'dec: 45/oct: 055/hex: 0X2D' >>> "MM/DD/YY = %02d/%02d/%02d" % (12, 7, 41) 'MM/DD/YY = 12/07/41' >>> 'Total with tax: $%.2f' % (13.00 * 1.0825) 'Total with tax: $14.07' >>> d = {'web': 'user', 'page': 42} >>> 'http://xxx.yyy.zzz/%(web)s/%(page)d.html' % d 'http://xxx.yyy.zzz/user/42.html'
以下是等效的代码片段,但使用str.format():
>>> "Name: {0}, age: {1}".format('John', 35) 'Name: John, age: 35' >>> i = 45 >>> 'dec: {0}/oct: {0:#o}/hex: {0:#X}'.format(i) 'dec: 45/oct: 0o55/hex: 0X2D' >>> "MM/DD/YY = {0:02d}/{1:02d}/{2:02d}".format(12, 7, 41) 'MM/DD/YY = 12/07/41' >>> 'Total with tax: ${0:.2f}'.format(13.00 * 1.0825) 'Total with tax: $14.07' >>> d = {'web': 'user', 'page': 42} >>> 'http://xxx.yyy.zzz/{web}/{page}.html'.format(**d) 'http://xxx.yyy.zzz/user/42.html'
与Python2.6+一样,所有Python3发行版(到目前为止)都了解如何同时执行这两种操作。我不知羞耻地把这些东西直接从my hardcore Python intro book和介绍+中间Python courses I offer的幻灯片中撕了出来。:-)
:-)
2018年8月更新:当然,现在我们有了the f-string feature in 3.6,我们需要的等价示例,即,是的,另一种选择:
>>> name, age = 'John', 35 >>> f'Name: {name}, age: {age}' 'Name: John, age: 35' >>> i = 45 >>> f'dec: {i}/oct: {i:#o}/hex: {i:#X}' 'dec: 45/oct: 0o55/hex: 0X2D' >>> m, d, y = 12, 7, 41 >>> f"MM/DD/YY = {m:02d}/{d:02d}/{y:02d}" 'MM/DD/YY = 12/07/41' >>> f'Total with tax: ${13.00 * 1.0825:.2f}' 'Total with tax: $14.07' >>> d = {'web': 'user', 'page': 42} >>> f"http://xxx.yyy.zzz/{d['web']}/{d['page']}.html" 'http://xxx.yyy.zzz/user/42.html'
可以使用format()函数,因此在您的情况下可以使用:
format()
return '{:02d}:{:02d}:{:.2f} {}'.format(hours, minutes, seconds, ampm)
有多种方法可以使用此函数,因此有关详细信息,可以检查documentation。
f-strings是Python 3.6中添加到语言中的一个新特性。这有助于格式化字符串:
return f'{hours:02d}:{minutes:02d}:{seconds:.2f} {ampm}'
从Python 3.6开始,Python中的格式化可以使用formatted string literals或f-strings完成:
hours, minutes, seconds = 6, 56, 33 f'{hours:02}:{minutes:02}:{seconds:02} {"pm" if hours > 12 else "am"}'
或者从2.7开始的^{}函数:
"{:02}:{:02}:{:02} {}".format(hours, minutes, seconds, "pm" if hours > 12 else "am")
或者string formatting ^{} operator用于更旧版本的Python,但请参见文档中的注释:
"%02d:%02d:%02d" % (hours, minutes, seconds)
对于格式化时间的具体情况,有^{}:
import time t = (0, 0, 0, hours, minutes, seconds, 0, 0, 0) time.strftime('%I:%M:%S %p', t)
从Python2.6开始,有一个替代方法:方法
str.format()
。下面是一些使用现有字符串格式运算符(%
)的示例:以下是等效的代码片段,但使用
str.format()
:与Python2.6+一样,所有Python3发行版(到目前为止)都了解如何同时执行这两种操作。我不知羞耻地把这些东西直接从my hardcore Python intro book和介绍+中间Python courses I offer的幻灯片中撕了出来。
:-)
2018年8月更新:当然,现在我们有了the f-string feature in 3.6,我们需要的等价示例,即,是的,另一种选择:
Python2.6+
可以使用
format()
函数,因此在您的情况下可以使用:有多种方法可以使用此函数,因此有关详细信息,可以检查documentation。
Python3.6+
f-strings是Python 3.6中添加到语言中的一个新特性。这有助于格式化字符串:
从Python 3.6开始,Python中的格式化可以使用formatted string literals或f-strings完成:
或者从2.7开始的^{} 函数:
或者string formatting ^{} operator 用于更旧版本的Python,但请参见文档中的注释:
对于格式化时间的具体情况,有^{} :
相关问题 更多 >
编程相关推荐