python字符串

python中的两种格式化打印:

1) 类似于C的printf的打印方式,%

  print “my name is %s” % ("hym")

  其他的控制格式化输出的特殊符号:%c,%d,%u,%o,%x(小写十六进制),%X(大写十六机制),%f

2) 类似于C#的格式化输出方式,.format

  print '{0} {1}:{2}'.format('hello', '1', '7')

    输出hello 1:7

  print 'test:{0:10f}'.format(math.pi)

    输出3.141593

string字符串的内建函数:

  1) string.decode(encoding="utf-8", errors='strict'),改变字符串的编码方式;

    print  string.decode('utf-8')

  2) string.split(str="pattern", num)[num],以pattern为边界,分割n次string,拿第几个元素返回;

    string.split('hello')[-1],拿到hello之后的字符串

  3) string.strip(' '),删除字符左右边的空格,删除string后的换行符;

  4) string.uppper(),string.lower()

  5) string.join(seq),以string为分隔符,将seq合并为一个大的string

两个字符串的比较,str1 == str2

python中字符的编码方式:

  计算机中有两种的字符编码方式:ASCII(只能表示英文和一些特殊字符)

                   UTF-8,UTF-16(世界语言通用的编码方式)

ASCII是很早之前ANSI推出的英文的编码方式,之后为了加入汉语,我国自己扩展了国标标准,GBK标准,(GB2312---GB18030)

    之后,各个国家相继退出自己的编码标准,ISO组织为了统一处理,提出了Unicode字符集,每个国家的语言都能

    在其中找到自己的码位,UTF-8和UTF-16是Unicode具体的编码方式,来方便在互联网中传播。

原文地址:https://www.cnblogs.com/-9-8/p/8251257.html