打开 IDLE (python shell)
>>> print ("hello world") # 输出字符串
hello world
>>> print (123) # 输出数字
123
>>> L = [1,2,'abc'] # 列表
>>> print(L)
[1, 2, 'abc']
>>> t = (1,2,'abc') # 元组
>>> print(t)
(1, 2, 'abc')
>>> d = {'a':1, 'b':2} # 字典
>>> print(d)
{'a': 1, 'b': 2}
格式化输出:
>>> str = "the length of (%s) is %d" %('helloworld',len('helloworld'))
>>> print(str)
the length of (helloworld) is 10
>>>
python字符串格式化符号:
符号 描述
%c 格式化字符及其ASCII码
%s 格式化字符串
%d 格式化整数
%u 格式化无符号整型
%o 格式化无符号八进制数
%x 格式化无符号十六进制数
%X 格式化无符号十六进制数(大写)
%f 格式化浮点数字,可指定小数点后的精度
%e 用科学计数法格式化浮点数
%E 作用同%e,用科学计数法格式化浮点数
%g %f和%e的简写
%G %f 和 %E 的简写
%p 用十六进制数格式化变量的地址
格式化输出浮点数(float)
>>>pi = 3.141592653
>>> print('%10.3f' % pi) #字段宽10,精度3
3.142
>>> print("pi = %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度
pi = 3.142
>>> print('%010.3f' % pi) #用0填充空白
000003.142
>>> print('%-10.3f' % pi) #左对齐
3.142
>>> print('%+f' % pi) #显示正负号
+3.141593
print 不换行
在 Python 中 print 默认是换行的
>>>for i in range(0,3):
... print (i)
...
0
1
2
>>>
要想不换行可以应该写成 print(i, end = ‘’)
>>> for i in range(0,5):
print(i,end='')
01234
>>>