如何正确使用Python .format()? [英] How to use Python .format() appropriately?

查看:79
本文介绍了如何正确使用Python .format()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下内容:

user_ = socket.gethostname()
runtime_ = time.ctime()
game_days = 'season open'
variable = 'speed'
start_time = datetime.time(15, 0)
weekdays = 'all days'
period = ['1/1/2013', '1/1/2020']

如何使用 .format()参见.为了得到这个输出

How can I use .format() see this. In order to get this output

print(tag)

User-WS, Mon Mar 27 07:30:08 2017  US/Central - season open
Variable: speed At: 15:00. all days 1/1/2013:1/1/2020
--------------------------------------------------------

现在我正在做

tag = user_ + ', ' + runtime_ + '  US/Central - ' + game_days + '\n' + 'Variable: '  + \ 
variable + ' At: ' + str(start_time)[:-3] + '. ' + weekdays + ' ' + period[0] + ':' + period[1] + '\n' + \
'--------------------------------------------------------'

推荐答案

s = '{}, {}, US/Central - {}\nVariable: {}, At: {}. {} {}:{}\n--------------------------------------------------------'

tag = s.format(user_, runtime_ game_days, variable, str(start_time)[:-3], weekdays, period[0],period[1])

这是您链接的非常详尽的教程,我不知道其中的某些内容.您在努力挣扎的是什么?

That is a remarkably thorough tutorial you linked, I didn't know some of that. What was it specifically you were struggling with?

这篇关于如何正确使用Python .format()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆