4

I am using Python 3 and am trying to use my print statements with the str.format.

e.g:

print ('{0:3d} {1:6d} {2:10s} '.format (count1,count2,string1)) 

When I try to use the end='' to suppress the subsequent newline, this is ignored. A newline always happens.

How do I suppress the subsequent newline?

Source:

int1= 1
int2 = 999
string1 = 'qwerty'
print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1))
print ('newline')
print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1,end=''))
print ('newline')
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "copyright", "credits" or "license()" for more information.

1 999 qwerty
newline

1 999 qwerty
newline

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
pe9298
  • 43
  • 1
  • 4
  • Can you show your code that you used when you attempted to use the `end` argument? – skrrgwasme Apr 11 '15 at 22:48
  • @skrrgwasme - the code is added above. The output somehow got split, but it is the same both for both print statements. – pe9298 Apr 13 '15 at 19:36

1 Answers1

5

Your problem is that you have the end='' argument being passed to the format function, not to the print function.

Change this line:

print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1,end=''))

To this:

print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1), end='')

By the way, you should also give PEP8 a read. It defines standards for Python coding styles, that you really should try to follow, unless you're working with a group of people that have agreed on some other style standards. In particular, your spacing is a bit weird around function calls - you shouldn't have spaces between function names and the argument parentheses, or between the parentheses and the first argument. I wrote my suggested solution to your problem in a way that maintains your current style, but it really should look more like this:

print('{0:3d} {1:6d} {2:10s} '.format(int1, int2, string1), end='')
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84