0

When I use the code in python 2.7.5

for i in [1,2]:
       print 'distance',':400@CA',':',i,'@CA'

I got the following

distance :400@CA : 1 @CA

distance :400@CA : 2 @CA

I want to remove the space (eg., among :,1,@CA) so that the output will be like

distance :400@CA :1@CA

distance :400@CA :2@CA

I also tried using sep='' or end='' but still they don't work. Any suggestion will be appreciated.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
Mahendra Thapa
  • 163
  • 2
  • 4
  • 12

4 Answers4

2

I'd use the string formatting operator %:

for i in [1,2]:
   print 'distance :400@CA :%d@CA' % i

This gives you quite a lot of control over how things are laid out.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

You can use %d operator:

for i in [1,2]:
       print 'distance',':400@CA',':','%d@CA' % i

Or you can use join:

for i in [1,2]:
           print 'distance'+':400@CA:'.join(i,'@CA')

With reference to:How to print a string of variables without spaces in Python (minimal coding!)

Hope this helps...

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276
0
  for i in [1,2]:
           print 'distance',':400@CA',':'+str(i)+'@CA'

Output: distance :400@CA :1@CA
        distance :400@CA :2@CA

Using ,(comma) will add space while printing. Better concatenate using +

Sesha
  • 202
  • 1
  • 5
0

Use the sep tag in print

from __future__ import print_function
print('distance',':400@CA',':',i,'@CA', sep = '')
Ishan Garg
  • 178
  • 2
  • 11