1、 计算UTF-8/GBK编码的字符串长度
str1 = '人生苦短,我用Python!' length = len(str1.encode()) length1 = len(str1.encode('gbk')) print(length)print(length1)
2、分割字符串(split)
str1 = '人 生 苦 短 >>> 我 用 P y t h o n ! >>> csdn.com'print('原字符串:',str1)list1 = str1.split() list2 = str1.split('>>>') list3 = str1.split('.') list4 = str1.split(' ',4) print(str(list1) + '\n' + str(list2) + '\n' + str(list3) + '\n' + str(list4))list5 = str1.split('>') print(list5)
3、格式化字符串%
template = '编号:%.4d\t公司名称: %s \t官网: http://www.%s.com'context1 = (7,'百度','baidu')context2 = (8,'明日学院','mingrisoft')print(template%context1)print(template%context2)
4、格式化字符串{:}
template = '编号:{:0>9s}\t公司名称: {:s} \t官网:/' \ ' http://www.{:s}.com'context1 = template.format('7','百度','baidu')context2 = template.format('10','明日学院','mingrisoft')print(context1)print(context2)