8.5 字符串
什么是字符串?
具有特殊意义的一些字符拼接在一起的串
字符串是数组
像许多其他流行的编程语言一样,Python 中的字符串是表示 unicode 字符的字节数组。
但是,Python 没有字符数据类型,单个字符就是长度为 1 的字符串。
方括号可用于访问字符串的元素。
字符定义?
字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。
创建字符串很简单,只要为变量分配一个值即可
单引号
双引号
三引号
str() s= str("")
Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。
Python 访问子字符串,可以使用方括号来截取字符串
常用方法:
‘capitalize’, ‘casefold’, ‘center’, ‘count’, ‘encode’, ‘endswith’, ‘expandtabs’, ‘find’, ‘format’,
‘format_map’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isascii’, ‘isdecimal’, ‘isdigit’, ‘isidentifier’, ‘islower’,
‘isnumeric’, ‘isprintable’, ‘isspace’, ‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘maketrans’,
‘partition’, ‘removeprefix’, ‘removesuffix’, ‘replace’, ‘rfind’, ‘rindex’, ‘rjust’, ‘rpartition’, ‘rs