python format函数换行,禁止以python 3 str.format打印换行符

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

解决方案

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='')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值