0
考慮以下幾點:如何正確使用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()
看到this。爲了得到這個輸出
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' + \
'--------------------------------------------------------'
你有什麼試過?一般來說,只需用佔位符替換當前的''+ name +'',就可以從串聯轉爲格式化,因此您只有一個字符串,然後將所有名稱傳遞給'.format'。這沒有用嗎? – jonrsharpe