Reputation: 79
I have write down a python code given bellow, i was trying to make same changes to remove the space from out put but its not working for me :
text = "000000000000000000000000000000100000000000000000000100000000000001010000000000"
result = []
for item in enumerate(text):
i, ch = item
if ch == '1':
result.append(i+1)
x = result
for i in xrange(len(x)):
print i+1,':',x[i],
output of this code is:
1 : 24 2 : 37 3 : 45 4 : 56 5 : 66
i want to make some changes so that i could produce output like this:
1:24 2:37 3:45 4:56 5:66
Upvotes: 0
Views: 37
Reputation: 251001
Use string formatting:
for i, x in enumerate(result, start=1):
print '{}:{}'.format(i, x),
#output: 1:31 2:52 3:66 4:68
And instead if using xrange(len(x))
for indexing use enumerate
.
enumerate
also allows us to provide the start
index, so the first loop in your code can be changed to:
for i, ch in enumerate(text, start=1):
if ch == '1':
result.append(i)
Upvotes: 4