user1765661
user1765661

Reputation: 595

Python suffix issue with xrange

How can I 'format' my iterator in xrange to add a "normalized" suffix? Like "nice_name_1","nice_name_2", etc. My current iterator adds by 3 since I am using other for loop inside. Thank you

objs = ['pencil','pen','keyboard','table','phone']



for i in xrange(0, len(objs), 3):

    #...

    for n, obj in enumerate(objs[i:i+3]):
        print '...'

    #...
    print 'nice_name_' + str(i)

# Result:
...
...
...
nice_name_0
...
...
nice_name_3

Upvotes: 0

Views: 46

Answers (1)

Hugh Bothwell
Hugh Bothwell

Reputation: 56624

print 'nice_name_' + str(i // 3)

or preferably

print('nice_name_{}'.format(i//3 + 1))  # start counting at 1

Upvotes: 1

Related Questions