定义:
由独立字符组成的一个序列,通常包含在单引号(’’)、双引号(””)、三引号(’’’ ‘’’或 “”” “””)之中。三种表达方式方便内嵌带引号的字符串,其中三引号通常用于多行字符串,如函数注释。
操作:
支持索引、切片、遍历等操作。
字符串是不可变的,因此,改变一个字符串内部字符会报错。s = 'hello's[0] = 'H' // TypeError
改变字符串的方式:
1)s = 'H' + s[1:]
2) s = s.replace('h', 'H')
3) ‘+=’ 操作符打破了字符串不可变的特性,且时间复杂度为O(n)
4)字符串拼接还有字符串内置 join 函数。
格式化:
1)print('person info, id: {}, name: {}.'.format(id, name))
2)print('person info, id: %s, name: %s.' % (id, name))
