Trying to make a program that displays "Harry Verderchi" 7 times using the for-loop. With the number preceding the name like:

#2: Harry ....

name = "Harry"
name2 = "Verderchi"

for i in range(8):
print "#" + "i" + ": " + name + " " + name2

name = "Harry"
name2 = "Verderchi"

for i in range(7):
print "#" + range(i) + ": " + name + " " + name2


2nd try

name = str("Harry Verderchi")

for i in range(7):
print "#" + str(range(1, 8)) + ": " + name

I realize im a 'newb' to your forum...but jeez..help a white boiii out please

name = str("Harry Verderchi")

for i in range(1,8):
print "#" + str(i) + ": " + name


Thanks again..ME

a simpler way, without having to convert i to a string could be:

name='harry'
name2='verderchi'
nt=8 #number of times to print
for i in range(1,nt+1):
    print '#%s:%s %s'%(i, name, name2)

the %s is a placeholder for the variables inside the brackets.

An often overlooked feature of Python is that you can format print directly from a dictionary ...

# using the local dictionary vars() to print variables

name = "Harry"
name2 = "Verderchi"

for i in range(8):
    # %d for integers and %s for strings
    print "#%(i)d: %(name)s %(name2)s" % vars()

"""
my result  -->
#0: Harry Verderchi
#1: Harry Verderchi
#2: Harry Verderchi
#3: Harry Verderchi
#4: Harry Verderchi
#5: Harry Verderchi
#6: Harry Verderchi
#7: Harry Verderchi
"""

Not seen very often, but very useful is string templating ...

# using string.Template() and the local dictionary vars()
# for formatted printing

import string

name = "Harry"
name2 = "Verderchi"

# create the template (note the $ before each variable name)
t = string.Template("#$i: $name $name2")

for i in range(8):
    print t.safe_substitute(vars())

"""
my output -->
#0: Harry Verderchi
#1: Harry Verderchi
#2: Harry Verderchi
#3: Harry Verderchi
#4: Harry Verderchi
#5: Harry Verderchi
#6: Harry Verderchi
#7: Harry Verderchi
"""
This question has already been answered. Start a new discussion instead.