2020str = r'Hello \' \t \n Jiaming'
2121print (str )
2222print (r'''Hello,\n
23- World''' )
23+ World''' )
24+
25+ # 字符编码
26+ # 字符编码和字符转换
27+ print ('-------------------------------------' )
28+ print (ord ('A' ))
29+ print (chr (25991 ))
30+ # print('\u4e2d\u6587')
31+
32+ # 字符串和字节类型数据 bytes 转换
33+ # bytes类型的数据以 b为前缀的单引号或者双引号表示
34+ print ('-------------------------------------' )
35+ ss = b'ABC'
36+ print (ss )
37+
38+ # encode() 指定字符集编码(为字节) 和decode() 指定解码字符集(为字符)
39+ str = 'ABCD'
40+ print (str .encode ('ascii' ))
41+ str = '你好啊'
42+ print (str .encode ('utf-8' ))
43+
44+ strbyte = b'\xe4 \xbd \xa0 \xe5 \xa5 \xbd \xe5 \x95 \x8a '
45+ print (strbyte .decode ('utf-8' ))
46+
47+ # len() 长度计算
48+ # print(len(12)) object of type 'int' has no len()
49+ print (len ('ABC' )) # 计算字符串长度
50+ print (len ('你好啊' .encode ('utf-8' ))) # 计算字节长度
51+
52+ # -*- coding:utf-8 -*- 告诉python解释器按UTF-8编码读取源码
53+ # windows系统保存源码选择 UTF-8 without BOM
54+
55+ # 格式化 python字符串格式化和c语言一致。
56+ # %[(name)][flags][width].[precision]typecode
57+ # []表示可选
58+ # (name) :指定key值
59+ # flags :+ 右对齐,正数前加正号,负数前加负号; - 左对齐,正数无符号,负数加负号; 空格,右对齐,正数加空格,负数加负号; 0,右对齐,正数无符号,负数加负号,0填充空白。
60+ # width :占位宽度
61+ # .precision :小数精度
62+ # typecode :s 字符 d 整数 f 浮点数
63+ print ('-------------------------------------' )
64+ print ('hello %s, I\' m %d and My height is %.2f m' % ('Jiaming' , 20 , 1.75123 ))
65+ print ('hello %(name)s, I\' m %(age)d and My height is %(height)05.2f m' % {'name' : 'Jiaming' , 'age' : 20 , 'height' : 1.75123 })
66+
67+ # format
68+ # [[fill]align][sign][#][0][,][.precision][type] 用 : 代替 %
69+ print ('-------------------------------------' )
70+ print ('----{:*^20s}----' .format ('welcome' ))
71+ print ('My name is {name:s}, I\' m {age:d} now and My Height is {height:,.2f}' .format (name = 'Jiaming' , age = 21 , height = 1111.987 ))
72+
0 commit comments