美文网首页Python学习笔记
Python的输出和输入

Python的输出和输入

作者: Viking_Den | 来源:发表于2016-08-24 23:04 被阅读143次

Python的输出

常见的Python输出是使用print内建函数,或者输出到文件,或者输出到数据库,本文的输出主要是介绍print函数,下面是使用help方法查看print的说明:


help(print).png

从上图可以看出,print函数的默认分隔符是一个空格,默认在最后加上换行符。

简单输出:
Python简单输出.png
格式化输出:
python格式化输出.png
format()函数:
  1. 通过位置替换

相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’。

>>> print ('{0} {1}'.format('hello','world'))
hello world  
>>> print ('{} {}'.format('hello','world'))
hello world  
>>> print ('{0} {1} {0}'.format('hello','world'))
hello world hello  

在字符串模板中确定位置,并且位置可以不按顺序,format()可传入任意数目的参数。

  1. 关键字替换

也可以采用关键字替换的方法。

>>> print ('i love {python}'.format(python='you'))  
i love you 
  1. 其他使用方法

如可以指定输出长度和输出的对齐方式,其中对齐方式有一下几种:

  • < (默认)左对齐
  • >右对齐
  • ^ 中间对齐
  • = (只用于数字)在小数点后进行补齐
>>> print (format(3.14151617,'.5f')) 
3.14152  
>>> print ('{0:>10}'.format('sqxu'))    #10个占位符,右对齐  
      sqxu  
>>> print ('{0:4.2f}'.format(3.141516))
3.14  
>>> print ('{0:6.2f}'.format(3.141516)) 
  3.14  
>>> print ('{0:>6.2f}'.format(3.141516)) 
  3.14  
>>> print ('{1:<10},{0:<15}'.format('sqxu','USTC'))  
USTC      ,sqxu             
>>> print ('name={name},age={age}'.format(name='sqxu',age=25))  
name=sqxu,age=25
如何让 print 不换行:

在Python中总是默认换行的.如果想要不换行,之前的 2.x 版本可以这样 print x, 在末尾加上 ,但在 3.x 中这样不起任何作用,要想换行你应该写成 print(x,end = '' )。

Python的输入

input函数,注意:python2.x版本对应为raw_input函数

python input function.png

接受一个标准输入数据,返回为string类型,prompt为提示信息。


input.png

相关文章

  • 2018-11-12day10-python2和python3

    python2和python3的区别 一、输入输出语句变成了输入输出函数 python2 --- print "...

  • Python的输入和输出

    我们做的程序为了有更好的交互效果,都是有输入和输出的,下面来说一下Python的输入和输出 Python的输入写法...

  • 第一个python程序

    第一个python程序 输入和输出 1. 输出 2. 输入 (1) input() 小结: 输入是Input,输出...

  • Python学习笔记1

    Python注释 Python变量 Python运算符 Python输入输出 输入函数 输出函数(3.x) ...

  • Python输出和输入

    输出和输入 输出 在python中输出用print()函数,也就是咱们所说的打印。 输出程序 上述程序运行结果 格...

  • Python输入和输出

    输入输出 input输入函数 input函数:获取用户输入,保存成一个字符串。重要的话,说两遍,input函数的返...

  • python输入和输出

    1. 输出格式化 使用字符串格式字面量,使用单引号或者(三倍单引号),在这个字符串当中,你可以使用Python表达...

  • Python输出和输入

    输出格式美化 str(): 函数返回一个用户容易读的表达形式。 repr():产生一个解释器易读的表达形式。

  • Python3入门(九)输入与输出

    前面几章介绍了一些常用的输入输出,本文将具体介绍Python的输入和输出 一、输出格式美化 Python两种输出值...

  • 二级Python---python语言的基本语法元素(Day1)

    一、基本输入输出函数 Python中有三个重要的基本输入、输出函数,用于输入、转换和输出,分别是input()...

网友评论

    本文标题:Python的输出和输入

    本文链接:https://www.haomeiwen.com/subject/tjmosttx.html