码迷,mamicode.com
首页 > 编程语言 > 详细

python输入与输出

时间:2018-07-30 23:32:50      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:负数   终端   逗号   1.2   方式   位置   括号   表达式   赋值   

python输出

python3中的输出

python3中的输出使用函数print(),示例如下:

>>> print(‘hello kitty‘)

print()也可接受多个参数,使用逗号隔开:

>>> print(‘hello‘,‘kitty‘)
hello kitty

可以看到字符串合并输出后,中间会模式使用逗号隔开~

print函数除了可以接收字符串外,也可以接收其他的数据类型

>>> print(1)               # 接收整数
1
>>> print(1+2)           # 表达式
3
>>> print([1,2,3])       # 列表
[1, 2, 3]
>>> print({‘a‘:1,‘b‘:2})  # 字典
{‘a‘: 1, ‘b‘: 2}

python2中的输出

python2中的输出使用print 加上 输出数据,示例如下:

>>> print ‘hello kitty‘

也可以接收多个参数:

>>> print ‘1+2 =‘,3
1+2 = 3

用法与python3中的print()函数基本相同~

python格式化输出

格式化输出字符串

>>> print(‘My name is %s‘ % (‘abc‘))
My name is abc

%表示格式化操作,% 前面的字符串中的%s(格式符) 使用 % 后面的字符串 ‘abc‘ 替换。

打印整数:

>>> print("I‘m %d year old" % 18)     # 当只有一个值的时候,可以不适用小括号
I‘m 18 year old

多个格式符:

>>> print("I‘m %s. I‘m %d year old" % (‘abc‘, 18))
I‘m abc. I‘m 18 year old

多个格式符也可以使用字典来传递值:

>>> print("I‘m %(name)s. I‘m %(age)d year old" % {‘name‘:‘abc‘, ‘age‘:18})
I‘m abc. I‘m 18 year old

格式符

%s    字符串 (采用str()的显示)
%r    字符串 (采用repr()的显示)
%c    格式化字符及其ASCII码
%b    二进制整数
%d    十进制整数
%u    格式化无符号整型
%o    格式化无符号八进制数
%x    格式化无符号十六进制数
%X   格式化无符号十六进制数(大写)
%e    用科学计数法格式化浮点数
%E    作用同%e,用科学计数法格式化浮点数
%f     格式化浮点数字,可指定小数点后的精度
%g    %f和%e的简写
%G    %f 和 %E 的简写
%%    字符"%"

?

格式符为真实值预留位置,并控制显示的格式。

可以用如下的方式,对格式进行进一步的控制:
%[(name)][flags][width].[precision]typecode
(name)为命名
flags可以有-,‘ ‘或0。若不写默认表示右对齐。- 表示左对齐。‘ ‘为一个空格,表示在正数的左侧填充一个空格,从而与负数 对齐。0表示使用0填充。
width表示显示宽度
precision表示小数点后精度

示例如下:

>>> print("%4d" % 5)                 #  flags不填(默认右对齐),width为4(总长为4位)
   5

>>> print("%-4d" % 5)               #  flags为 - ,表示左对齐
5

>>> print("%06d" % 5)               # 总长为6位,flags为0,即左边使用0填充
000005

>>> print(‘-- %f  --‘ % (1.23))       # 格式化浮点数
-- 1.230000  --

>>> print(‘-- %5.2f  --‘ % (1.2345))     # 总长5位,小数点后保留2位
--  1.23  --

>>> print(‘-- %05.2f  --‘ % (1.2345))    # 总长5位,小数点后保留2位,flags为0,左边使用0填充(小数点也占一位)
-- 01.23  --

Python中还有另一种格式化方式,利用format,这也是官方推荐的方式:

方式一:
>>> print("My name is {0}. I‘m {1} year old. Hello {0} !!".format(‘baby‘, 18))
My name is baby. I‘m 18 year old. Hello baby !!

方式二:
>>> print("My name is {name}. I‘m {age} year old. Hello {name} !!".format(name=‘baby‘, age=18))
My name is baby. I‘m 18 year old. Hello baby !!

python输入

python3中的输入

python3中的输入使用input(),将用户在终端的输入,存放到一个变量中

>>> name=input()
hello
>>> name
‘hello‘

input() 可以带上一个参数,作为用户输入时的提示信息,示例如下:

>>> name = input("What is your name?")
What is your name?abc
>>> name
‘abc‘

Tip:input() 会将用户输入的数据都当做字符串(str)进行处理~

>>> lst = input()
[1,2,3,4,5]
>>> type(lst)
<class ‘str‘>

python2中的输入

python2中的raw_input用法与python3中的input() 类似:

>>> age = raw_input("How old are you?")
How old are you?12
>>> type(age)
<type ‘str‘>

Tip:raw_input也一样,会将用户输入的数据都当做字符串(str)处理。

python2中还可以用 input() 来接收用户的输入,这里的 input() 用法与python3中的 input() 有所区别

>>> name = input("What is your name?")
What is your name?baby                                # 这里输入的是 变量 baby,而不是字符串,由于 baby 变量没有定义,所以报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name ‘baby‘ is not defined
>>> name = input("What is your name?")
What is your name?‘baby‘                             # 这里输入的是 字符串 ‘baby‘,成功赋值~

>>> lst = input()
[1,2,3,4,5]                                                      # 输入的是 列表类型,lst变量即为列表~
>>> type(lst)
<type ‘list‘>

Tip:python2中的 input() 在接收用户输入的数据时,输入的是什么类型,就存放为什么类型。注意区别
.................^_^

python输入与输出

标签:负数   终端   逗号   1.2   方式   位置   括号   表达式   赋值   

原文地址:http://blog.51cto.com/ljbaby/2152362

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!