8 Python基本数据类型---字符串
1 字符串的定义与创建 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息 s1 = ' 单引号 ' s2 = " 双引号 " s3 = ''' 三引号 ''' s4 = """ 三引号 """ 2 字符串的特性与常用操作 特性: 有序不可变 补充: 1.字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r'l\thf' 2.unicode字符串与r连用必需在r前面,如name=ur'l\thf' 常用操作: #索引 s = 'hello' >>> s[1] 'e' >>> s[-1] 'o' >>> s.index('e') 1 #查找 >>> s.find('e') 1 >>> s.find('i') -1 #移除空白 s = ' hello,world! ' s.strip() s.lstrip() s.rstrip() s2 = '***hello,world!***' s2.strip('*') #长度 >>> s = 'hello,world' >>> len(s) 11 #替换 >>> s = 'hello world' >>> s.replace('h','H') 'Hello world' >>> s2 = 'hi,how are you?' >>> s2.replace('h','H')