0

Given the following:

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']

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
--------------------------------------------------------

Right now I'm doing:

tag = user_ + ', ' + runtime_ + '  US/Central - ' + game_days + '\n' + 'Variable: '  + \ 
variable + ' At: ' + str(start_time)[:-3] + '. ' + weekdays + ' ' + period[0] + ':' + period[1] + '\n' + \
'--------------------------------------------------------'
1
  • 1
    What have you tried? In general you can go from concatenation to formatting just by replacing the current ' + name + ' with a placeholder, so you have a single string, then passing all the names to .format. Did that not work? Commented Mar 27, 2017 at 12:39

1 Answer 1

2
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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.