字符串
1.定义,字符串是一个有序的字符集合,用于表示和储存基本的文本信息。
name = 'harris 'name = "harris " #有引号的就是字符串name = """harrishahha""" #三引号表示多行
2.字符串拼接
可使用 + 和 *
p1 = "你好“p2 = "harris"print(p1+ p2)print(p1 *3)
特性:
1.按照从左到右的顺序定义字符集合,下标从0开始顺序访问,有序
补充:
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')'Hi,How are you?'#切片>>> s = 'abcdefghigklmn'>>> s[0:7]'abcdefg'>>> s[7:14]'higklmn'>>> s[:7]'abcdefg'>>> s[7:]'higklmn'>>> s[:]'abcdefghigklmn'>>> s[0:7:2]'aceg'>>> s[7:14:3]'hkn'>>> s[::2]'acegikm'>>> s[::-1]'nmlkgihgfedcba'
