python2.7如何让print“不换行”

如题哈,用python打印URL的网页信息是,想让其自然换行,也就是打印完一句后在换行,但如果只是用print,则打印完每个字母就换行,如果用加逗号的那种方式,虽然可以解决换行问题,但每个字母中间会自动的加一个空格,看着也很是不自然,那么怎么才能自然的打印出url的网页信息呢?
【目前代码如下】
#This is the python test
import urllib;
print "Program is working............";
fileName="网址";
fileHandle=urllib.urlopen(fileName);
fileData=fileHandle.read();
fileHandle.close();
for tempLine in fileData:
print tempLine,;
print "Program is over!";

Python 2.x 让 print “不换行”的方法是在句尾加上逗号,比如:

print 'Hello' :会换行;

print 'Hello',  :不会换行。

或者直接采用:

from __future__ import print_function

print('go ', end='')

print('home', end='')

Python 3.x 则对这个语法进行了修改。

print ('Hello') 默认也会换行,但是可以指定一个 end 参数来表示结束时输出的字符:print ('Hello', end = ' ') 就表示输出 Hello 之后会再输出一个空格;print ('Hello', end = '') 则表示输出只输出 Hello,默认的 end 就是换行。

扩展资料

根据PEP的规定,必须使用4个空格来表示每级缩进(不清楚4个空格的规定如何,在实际编写中可以自定义空格数,但是要满足每级缩进间空格数相等)。

使用Tab字符和其它数目的空格虽然都可以编译通过,但不符合编码规范。支持Tab字符和其它数目的空格仅仅是为兼容很旧的的Python程序和某些有问题的编辑程序。

参考资料:百度百科-Python

                 CSDN-python2.7 print不换行的方法

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-03-25

Python 2.x通过在句子的末尾添加逗号来使print “不换行”,例如:

(1)print'Hello':换行;

(2)print'Hello',:不换行。

或直接如下:

from __future__ import print_function

print('go ', end='')

print('home', end='')

Python 3.x更改了此语法。

默认情况下,print('Hello')也将自动换行,但是用户可以指定一个end参数来指示末尾输出的字符:print('Hello',end ='')表示将在Hello之后输出一个空格; print('Hello',end ='')表示输出仅是Hello,默认结尾是换行符

扩展资料:

根据PEP规定,必须使用4个空格来指示每个级别的缩进(尚不清楚如何指定4个空格。在实际书写中,可以自定义空格的数量,但是每个级别缩进量之间的空格的数量必须相等)。

尽管可以编译和传递制表符和其他数量的空格,但其不符合编码规范。 但支持了制表符和其他数量的空格,以便与非常老的Python程序和某些有问题的编辑程序兼容。

本回答被网友采纳
第2个回答  推荐于2017-09-03

Python 2.x 让 print “不换行”的方法是在句尾加上逗号:

print 'Hello' :会换行;

print 'Hello',  :不会换行。

比如:

运行:


Python 3.x 则对这个语法进行了修改。print ('Hello') 默认也会换行,但是可以指定一个 end 参数来表示结束时输出的字符:print ('Hello', end = ' ') 就表示输出 Hello 之后会再输出一个空格;print ('Hello', end = '') 则表示输出只输出 Hello;默认的 end 就是换行。

第3个回答  推荐于2017-09-02
因为这句:fileData=fileHandle.read();把所有数据读回来,后变成一个字符串了,要么你直接这样打印:
print(fileData)
要么改成下面这样:
#This is the python test
import urllib;
print "Program is working............";
fileName="网址";
fileHandle=urllib.urlopen(fileName);
fileData=fileHandle.readlines();#注意这里,改成readlines了
for tempLine in fileData:
print tempLine,;
fileHandle.close();#这个放后面再关
print "Program is over!";本回答被提问者和网友采纳
第4个回答  2012-08-22
.read() 拿到的结果是 字符串,对字符串用for循环,结果肯定是对每个字符都遍历一遍。

直接 .urlopen()以后的结果就可以直接 for 循环了,这样也能符合要求。
或者是那位所说的 .readlines()
相似回答