Python 2.x下的print语句在输出字符串之后会默认换行,如果不希望换行,只要在语句最后加一个“,”即可。但是在Python 3.x下,print()变成内置函数,加“,”的老方法就行不通了。

查询Python的Library Reference>Built-in Functions,找到如下条目

print([object...], *sep=' 'end='\n'file=sys.stdout)

Print object(s) to the stream file, separated by sep and followed by endsependand file, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objectis given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or Nonesys.stdout will be used. ”

其中,sep=''和end='\n'均是print()的关键参数,sep的默认值为空,end默认值为换行符,这就是print()在输出后默认换行的原因。相应的,解决办法就是对end赋值:print(something, something,.., end=''),使end值为空,这个换行就消除了。

原文地址:http://wpp9977777.blog.163.com/blog/static/46251007201192905622402/

Python 3.x下消除print()自动换行的更多相关文章

  1. python多线程场景下print丢失

    python多线程情况下,print输出会出现丢失的情况,而logging模块的日志输出不会. 以下是示例代码,多运行几次就会发现这个有意思的现象 # coding:utf-8 import thre ...

  2. 抓取oschina上面的代码分享python块区下的 标题和对应URL

    # -*- coding=utf-8 -*- import requests,re from lxml import etree import sys reload(sys) sys.setdefau ...

  3. python命令行下tab键补全命令

    在python命令行下不能使用tab键将命令进行补全,手动输入又很容易出错. 解决:tab.py #/usr/bin/env python # -*- coding:utf-8 -*- ''' 该模块 ...

  4. [转]关于python中带下划线的变量和函数的意义

    Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下,不同程序员编写的 Python 代码可以保持最大程度的相似风格.这样就易于阅读, ...

  5. python基础15下_迭代器_生成器

    print(dir([])) #告诉我列表拥有的所有方法 # 双下方法 # print([1].__add__([2])) print([1]+[2]) ret = set(dir([]))& ...

  6. python 查看目录下所有目录和文件

    python查看目录下所有的子目录和子文件 python递归遍历目录结构 我喜欢第一种 方法1 import json, os def list_dir(path, res): for i in os ...

  7. Python调用windows下DLL详解

    Python调用windows下DLL详解 - ctypes库的使用 2014年09月05日 16:05:44 阅读数:6942 在python中某些时候需要C做效率上的补充,在实际应用中,需要做部分 ...

  8. python 在windows下监听键盘按键

    python 在windows下监听键盘按键 使用到的库 ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数) 使用的Win32API SetWindowsHookEx(), ...

  9. python移动目录下所有子目录文件到新的总目录

    python移动目录下所有子目录文件到新的总目录 import os import shutil def file(p):     p=p     z=os.listdir(p)     for i ...

随机推荐

  1. MicroService/web Service/webAPI/RPC

    [TOC] 微服务 服务拆分,利用轻量化机制(通常为HTTP源API)实现通信,复杂度可控,独立部署,技术选型灵活,容错,扩展. 康威定律的实际体现 微服务架构模式深刻影响了应用和数据库之间的关系,不 ...

  2. Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  3. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  4. supervisor(三)xml_rpc

    supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的. xml_rpc其实就是本地可以去调用远端的函数方法,然后函数方法经过一番处理后,把结果返 ...

  5. AchartEngine 的学习

    第一步:我使用的事AchartEngine 1.1.0 的包.大家要先下在这个包,放到项目中,创建一个lib文件夹.然后倒金项目中去.然后再AndroidManifest.xml 中需要注册一下代码是 ...

  6. html php 重定向 跳转 刷新

    1秒后跳转 法一: <!DOCTYPE HTML> <html> <head>     <meta http-equiv="refresh" ...

  7. mysql root用户 远程登录其它机器,看不到数据库

    在102上访问101上的数据库里,show databases;看不到里面的库, 需要在101上授权就可以了 GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.16 ...

  8. 记一次小团队Git实践(中)

    对于初学者,从使用上先入手,往往学的最快,并从中汲取教训,再回头更深入的学习,效果尤佳. 安装git 安装git自不必说,mac已经内置了git,linux下一个命令就能搞定,windows下需要下载 ...

  9. SpringMVC解析2-ContextLoaderListener

    对于SpringMVC功能实现的分析,我们首先从web.xml开始,在web.xml文件中我们首先配置的就是ContextLoaderListener,那么它所提供了功能有哪些又是如何实现的?当使用编 ...

  10. uva624 01背包要求输出路径

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...