linux中Python环境下的输入、输出
1、Python2中的输入:input 和 raw_input##python2的输入[root@contral day01]# pythonPython 2.7.5 (default, Sep 12 2018, 05:31:16)[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2Type "help", "copyright", "...
·
1、Python2中的输入:input 和 raw_input
##python2的输入
[root@contral day01]# python
Python 2.7.5 (default, Sep 12 2018, 05:31:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input ('Num:') ##input仅支持整形
Num:1
1
>>> input ('Num:')
Num:a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
>>> raw_input ('Num:') ##raw_input 将所有输入转化为字符
Num:a
'a'
>>> raw_input ('Num:')
Num:1
'1'
若想对raw_input输入的整形进行处理,可以进行强制类型转化
##强制类型转化
>>> age = raw_input ('input your age:') ##输入的字符型
input your age:18
>>> age ##查看变量值
'18'
>>> type(age) ##查看变量类型
<type 'str'>
>>> int(age) ##强制类型转换
18
>>> type(age) ##转化后的变量类型并没有改变
<type 'str'>
>>> age
'18'
>>> type(int(age)) ##改变的是int(age)这个整体
<type 'int'>
>>>
2、Python3中得输入:input
##Python3中的输入
[root@contral day01]# python3
Python 3.7.7 (default, Apr 14 2020, 00:09:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input('input your age:')
input your age:^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> age = input ('input your ages:') ##只有input一种输入
input your ages:18
>>> age ##所有输入均以字符串对待
'18'
>>> type(age)
<class 'str'>
>>> type(int(age)) ##也可进行强制类型转换
<class 'int'>
>>>
3、输出:print
输出占位符:
##输出占位
%s ##字符型输出
%d ##整形输出
%f ##浮点型输出
%% ##百分号输出
%.nf ##保留n位小数,不足后面补0,小于全部输出
%.nd ##将整形输出补齐为n位,不足前面补0,小于全部输出
输出演示:
##输出演示
[root@servera ~]# ipython ##采用ipython交互的方式
Python 3.6.8 (default, Jan 11 2019, 02:17:16)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: name = 'linux'
In [2]: age = '11'
In [3]: print ('%s age is %d' %(name,age)) ##输入和输出的格式必须相同
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-47aecf82c254> in <module>
----> 1 print ('%s age is %d' %(name,age))
TypeError: %d format: a number is required, not str
In [4]: print ('%s age is %d' %(name,int(age)))
linux age is 11
In [5]: number = 123456.1234567
In [6]: print ('%s have %s money' %(name,number))
linux have 123456.1234567 money
In [7]: print ('%s have %f money' %(name,number)) ##字符和浮点数的输出
linux have 123456.123457 money
In [8]: print ('%s have %.2f money' %(name,number)) ##定义浮点数保留两位小数
linux have 123456.12 money
In [9]: print ('%s have %.2d money' %(name,number))
##定义保留整数位数,当整数位大于定义的数,则全部保留
linux have 123456 money
In [10]: num = 12
In [11]: print ('%s id is %d' %(name,num) )
linux id is 12
In [12]: print ('%s id is %.4d' %(name,num) ) ##定义的整数位不足是,前面补0
linux id is 0012
In [13]: print ('%s id is %.1d' %(name,num) )
linux id is 12
这里采用ipython交互式界面进行演示,ipython的安装在企业8中,可直接采用联网的方式进行安装:pip3 install ipython,普通安装可下载安装包,读取里面的RADEME,进行查看,或者查看官方文档均可。
更多推荐
所有评论(0)