知識點匯總;
1-字符串格式化輸出方法一: %
1-print('名字是 %s,年齡是%s' % (name ,age))
2- %s ---字符串-----相當於執行了str()
3- (name ,age) 只能是元組,不能是列表
4- 多個數據的打印,一定是元組
5- %d--十進制
6- %f--6位小數
7- %x--
8-指定長度打印----數值和字符串一樣的
1- %5d 右對齊 ,不足左邊補空格
2- -%5d 左對齊 ,不足右邊補空格
3- 補0 %05d
9- 十六進制:%#x # 加一個 0x
10 小數--float
1- 默認是6位
2- 指定保留小數位數- %.3f-----進行了四舍五入
3- %6.3f ---- 6代表總長度(包括 . )
4- %08.3f ---- 補0
2-字符串格式化輸出方法二: format()---固定的 {}
1- 順序填坑:
1- 可以有元素多,不能有元素少!
print('名字是 {},年齡是 {}'.format(name ,age))
2- 下標填坑:
1- 不能下標越界 IndexError: tuple index out of range
print('名字是 {1},年齡是 {0}'.format(name ,age))
3- 變量方法
1- print('名字是 {name},年齡是 {age}'.format(name='tom' ,age = 18))
4-指定長度輸出:
1- {:長度}
1- 數值型:右對齊,左補齊
2- 字符串:左對齊,右補齊
2- > 右對齊
3- < 左對齊
4- ^ 中間對齊 ---異或
5- 數值補0 ,一般是右對齊 , 左補0 ,不改變值
6- 字符串本身帶花括號 {{}}
3- python 3.6 以后 f''
print(f'名字是{name},年齡是{age}')
4- 轉義符 \
print('name is \n tom')
5- input()---控制台的終端輸入
1- 有返回值---str
2- 如果對得到的值進行算術---int()、float()
3- 用戶的輸入是以一個回車符結束---不敲回車就死等
'''
'''
1- format
1- 順序填坑
2- 下標填坑
3- 變量填坑 print('名字是{name},年齡是{age}'.format(name = 'tom',age = 18))
2- 中間對齊 ^
'''
name = 'tom'
age = 18
print(f'名字是:{name},年齡是:{age}')
fileDir1 = 'g:/test.py'
fileDir2 = 'g:\\file\\test.log'
fileDir3 = r'g:\file\test.log'
# print('名字是:{:>6},年齡是:{:0>6}'.format(name , age) )
# print('名字是:{1},年齡是:{0}'.format(name , age) )
# print('名字是:{name},年齡是:{age}'.format(name= 'tom' , age=18) )
'''
format:
1- 順序-print('名字是:{},年齡是:{}'.format(name , age) )
2- 下標填坑-print('名字是:{1},年齡是:{0}'.format(name , age) )
3- 變量填坑-print('名字是:{name},年齡是:{age}'.format(name= 'tom' , age=18) )
> 右對齊 {:0>6} < 左對齊 ^中間對齊
'''
# print('%06.3f' % 3.1415926)#%f----默認是6
# print(hex(108))
# print('%#x' % 108)
# print('%#X' % 108)
# print('%5d' % 56)
# print('名字是:'+name+' 年齡是:'+str(age))
#
# print('名字是:%s,年齡是:%d' % (name,age))# %s 格式- str
# str1 = '名字是:%s,年齡是:%d' % (name,age)
