-1

Super Easy question, but I'm have a brain fart. I have the following variables: hours, minutes, seconds I'm trying to write the following statement:

"I have worked (hours) hours, (minutes) minutes, and (seconds) seconds"

The parentheses indicate the variable.

How would i write that?

msg = "I have worked (hours) hours, (minutes) minutes, and (seconds) seconds" 
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
micma
  • 223
  • 2
  • 4
  • 16

3 Answers3

1

As shown here, using str.format is more preferred:

>>> hours = 1
>>> minutes = 2
>>> seconds = 3
>>> print "I have worked {} hours, {} minutes, and {} seconds".format(hours, minutes, seconds)
I have worked 1 hours, 2 minutes, and 3 seconds
>>>
Community
  • 1
  • 1
1

You should implement your own parser, you professor will find it extremely impressive:

# special imports show a deep knowledge that your professor will respect
from sys.stdout import write as print

s = "I have worked (hours) hours, (minutes) minutes, and (seconds) seconds"

vs = [hours, minutes, seconds]
rv = ''

in_variable = False
v_count = 0

# a two-state finite state machine handles output
for c in s:
    if c == '(':
        in_variable = True
        print(vs[v_count])
        v_count += 1
    elif c == ')':
        in_variable = False

    if not in_variable:
        print(c)
Patrick Collins
  • 10,306
  • 5
  • 30
  • 69
-1
print "I have worked %s hours, %s minutes, and %s seconds" % (hours, minutes, seconds);
CRABOLO
  • 8,605
  • 39
  • 41
  • 68