1. 输出:
python2: print "要打印的字符串"
python3: print("要打印的字符串")
# %s:代表字符串, %d: 整形, %f: 浮点型
>>> print("%s的年龄为%s" %(name, age))
westos的年龄为19
# .2f: 保留小数点后两位
>>> money = 7800.7812345660
>>> print("%s本月的公资为%f" %(name, money))
westos本月的公资为7800.781235
>>> print("%s本月的工资为%.2f" %(name, money))
westos本月的工资为7800.78
#.3d: 整形总占位数, 不够的前面补0
>>> sid = 1
>>> print("%s的学号为130%d" %(name, sid))
westos的学号为1301
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130001
>>> sid = 10
>>> print("%s的学号为130%.3d" %(name, sid))
westos的学号为130010
#输出中%是占位符, 输出%需要转义为 '%%'
举例:
print "内存占有率:%.2f%" %(1.23324546456)
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-21-5446e13955af>", line 1, in <module>
print "内存占有率:%.2f%" %(1.23324546456)
ValueError: incomplete format
print "内存占有率:%.2f%%" %(1.23324546456)
内存占有率:1.23%
print "内存占有率:%10.2f%%" %(1.23324546456)
内存占有率: 1.23%
print "内存占有率:%-10.2f%%" %(1.23324546456)
内存占有率:1.23 %
举例:
````````````
print "%s" %(001)
1
print "%d" %(001)
1
print "%d" %(0011)
9
print "%o" %(9)
11
print "%o" %(10)
12
print "%o" %(11)
13
print "%x" %(16)
10
print "%x" %(17)
11
print "%x" %(11)
b
print "%f" %(11)
11.000000
print "%.3f" %(11)
11.000
print "%.2f" %(11)
11.00
```````````
2. 输入:
# 输入:
*** python2:
- input:(只接受数值类型)
```
>>> help(input)
>>> input()
1
1
>>> num = input()
1
>>> num
1
>>> num = input("请输入密码:")
请输入密码:1234567
>>> import getpass
>>> num = getpass.getpass("请输入密码:")
请输入密码:
>>> print(num)
12345678
>>> num = input("请输入密码:")
请输入密码:westos123
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'westos123' is not defined
```
- raw_input(接收字符串类型)
```
>>> name = raw_input("请输入用户名:")
请输入用户名:westos
# 如果接收的值要进行数值比较时, 一定要转化为同种类型比较;
>>> age = raw_input("请输入年龄:")
请输入年龄:19
>>> type(age)
<type 'str'>
>>> age >19
True
>>> int(age) >19
False
```
*** python3
- input: 接收的为字符串数据类型, 没有raw_input
```
>>> num = input()
12
>>> name = input()
westos
>>> type(num)
<class 'str'>
>>> type(name )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'nam' is not defined
>>> type(name)
<class 'str'>
```