python3中的输出使用函数print(),示例以下:python
>>> print('hello kitty')
print()也可接受多个参数,使用逗号隔开:ide
>>> 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中的输出使用print 加上 输出数据,示例以下:code
>>> print 'hello kitty'
也能够接收多个参数:orm
>>> print '1+2 =',3 1+2 = 3
用法与python3中的print()函数基本相同~ip
格式化输出字符串ci
>>> print('My name is %s' % ('abc')) My name is abc
%表示格式化操做,% 前面的字符串中的%s(格式符) 使用 % 后面的字符串 'abc' 替换。字符串
打印整数:get
>>> 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 !!
python3中的输入使用input(),将用户在终端的输入,存放到一个变量中
>>> name=input() hello >>> name 'hello'
input() 能够带上一个参数,做为用户输入时的提示信息,示例以下:
>>> name = input("What is your name?") What is your name?abc >>> name 'abc'
当输入的内容为密码之类的数据时,能够使用getpass模块,隐藏输入的数据:
>>> import getpass >>> pwd=getpass.getpass('Enter your password:') Enter your password: # 不显示密码 >>> pwd 'abcd'
不过这个貌似只能在python的交互模式下才能调试,在pycharm中没法实现~
Tip:无论用户输入的数据看上去是什么类型的,input() 都会当作字符串(str)进行处理~
>>> lst = input() [1,2,3,4,5] >>> type(lst) <class 'str'> >>> lst '[1,2,3,4,5]' # 注意两边的单引号,这是一个字符串,而不是列表
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() 在接收用户输入的数据时,输入的是什么类型,就存放为何类型。注意区别.................^_^