20-字符串

字符串的定义

  • 字符串 就是 一串字符,是编程语言中表示文本的数据类型git

  • 在 Python 中能够使用 一对双引号 " 或者 一对单引号 ' 定义一个字符串编程

    • 虽然能够使用 \" 或者 \' 作字符串的转义,可是在实际开发中:api

      • 若是字符串内部须要使用 ",能够使用 ' 定义字符串编程语言

      • 若是字符串内部须要使用 ',能够使用 " 定义字符串ide

  • 能够使用 索引 获取一个字符串中 指定位置的字符,索引计数从 0 开始spa

  • 也能够使用 for 循环遍历 字符串中每个字符code

大多数编程语言都是用 " 来定义字符串orm

In [1]: hello_str.
hello_str.capitalize    hello_str.isidentifier  hello_str.rindex
hello_str.casefold      hello_str.islower       hello_str.rjust
hello_str.center        hello_str.isnumeric     hello_str.rpartition
hello_str.count         hello_str.isprintable   hello_str.rsplit
hello_str.encode        hello_str.isspace       hello_str.rstrip
hello_str.endswith      hello_str.istitle       hello_str.split
hello_str.expandtabs    hello_str.isupper       hello_str.splitlines
hello_str.find          hello_str.join          hello_str.startswith
hello_str.format        hello_str.ljust         hello_str.strip
hello_str.format_map    hello_str.lower         hello_str.swapcase
hello_str.index         hello_str.lstrip        hello_str.title
hello_str.isalnum       hello_str.maketrans     hello_str.translate
hello_str.isalpha       hello_str.partition     hello_str.upper
hello_str.isdecimal     hello_str.replace       hello_str.zfill
hello_str.isdigit       hello_str.rfind

1) 判断类型 - 9

方法 说明
string.isspace() 若是 string 中只包含空格,则返回 True
string.isalnum() 若是 string 至少有一个字符而且全部字符都是字母或数字则返回 True
string.isalpha() 若是 string 至少有一个字符而且全部字符都是字母则返回 True
string.isdecimal() 若是 string 只包含数字则返回 True,全角数字
string.isdigit() 若是 string 只包含数字则返回 True,全角数字\u00b2
string.isnumeric() 若是 string 只包含数字则返回 True,全角数字汉字数字
string.istitle() 若是 string 是标题化的(每一个单词的首字母大写)则返回 True
string.islower() 若是 string 中包含至少一个区分大小写的字符,而且全部这些(区分大小写的)字符都是小写,则返回 True
string.isupper() 若是 string 中包含至少一个区分大小写的字符,而且全部这些(区分大小写的)字符都是大写,则返回 True

2) 查找和替换 - 7

方法 说明
string.startswith(str) 检查字符串是不是以 str 开头,是则返回 True
string.endswith(str) 检查字符串是不是以 str 结束,是则返回 True
string.find(str, start=0, end=len(string)) 检测 str 是否包含在 string 中,若是 start 和 end 指定范围,则检查是否包含在指定范围内,若是是返回开始的索引值,不然返回 -1
string.rfind(str, start=0, end=len(string)) 相似于 find(),不过是从右边开始查找
string.index(str, start=0, end=len(string)) 跟 find() 方法相似,不过若是 str 不在 string 会报错
string.rindex(str, start=0, end=len(string)) 相似于 index(),不过是从右边开始
string.replace(old_str, new_str, num=string.count(old)) 把 string 中的 old_str 替换成 new_str,若是 num 指定,则替换不超过 num 次

3) 大小写转换 - 5

方法 说明
string.capitalize() 把字符串的第一个字符大写
string.title() 把字符串的每一个单词首字母大写
string.lower() 转换 string 中全部大写字符为小写
string.upper() 转换 string 中的小写字母为大写
string.swapcase() 翻转 string 中的大小写

4) 文本对齐 - 3

方法 说明
string.ljust(width) 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
string.rjust(width) 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
string.center(width) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

5) 去除空白字符 - 3

方法 说明
string.lstrip() 截掉 string 左边(开始)的空白字符
string.rstrip() 截掉 string 右边(末尾)的空白字符
string.strip() 截掉 string 左右两边的空白字符

6) 拆分和链接 - 5

方法 说明
string.partition(str) 把字符串 string 分红一个 3 元素的元组 (str前面, str, str后面)
string.rpartition(str) 相似于 partition() 方法,不过是从右边开始查找
string.split(str="", num) 以 str 为分隔符拆分 string,若是 num 有指定值,则仅分隔 num + 1 个子字符串,str 默认包含 '\r', '\t', '\n' 和空格
string.splitlines() 按照行('\r', '\n', '\r\n')分隔,返回一个包含各行做为元素的列表
string.join(seq) 以 string 做为分隔符,将 seq 中全部的元素(的字符串表示)合并为一个新的字符串
相关文章
相关标签/搜索