Python 字符串
字符串的定义
- 字符串 是 一串字符,也是
collection of character
, 因此,和列表、元组、字典有许多相同的方法特性。 - 在 Python 中可以使用 一对双引号
"
或者 一对单引号'
定义一个字符串- 虽然可以使用
\"
或者\'
做字符串的转义,但是在实际开发中:- 如果字符串内部需要使用
"
,可以使用'
定义字符串 - 如果字符串内部需要使用
'
,可以使用"
定义字符串
- 如果字符串内部需要使用
- 虽然可以使用
- 可以使用 索引 获取一个字符串中 指定位置的字符,索引计数从 0 开始
- 也可以使用
for
循环遍历 字符串中每一个字符
大多数编程语言都是用
"
来定义字符串
1 | string = "Hello Python" |
字符串的常用操作
- 在
ipython3
中定义一个 字符串,例如:hello_str = ""
- 输入
hello_str.
按下TAB
键,ipython
会提示 字符串 能够使用的 方法 如下:
1 | In [1]: hello_str. |
提示:正是因为 python 内置提供的方法足够多,才使得在开发时,能够针对字符串进行更加灵活的操作!应对更多的开发需求!
0) 字符串拼接
python 中进行字符串拼接,极其简单。 使用 +
连接两个字符串即可。
1 | In [5]: test = "hello"+"world" |
甚至直接去掉 +
, 直接 whitespace 也可。
1 | In [7]: test = "hello" "world" |
1) 判断类型 - 9
方法 | 说明 |
---|---|
string.isspace() | 如果 string 中只包含 whitespace,则返回 True |
string.isalnum() | 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True |
string.isalpha() | 如果 string 至少有一个字符并且所有字符都是字母则返回 True |
string.isdecimal() | 如果 string 只包含数字则返回 True, 全角数字 |
string.isdigit() | 如果 string 只包含数字则返回 True, 全角数字 、
⑴ 、 \u00b2 (注: 第二个是 unicode 字符串) |
string.isnumeric() | 如果 string 只包含数字则返回 True, 全角数字 ,
汉字数字 |
string.istitle() | 如果 string 是标题化的(每个单词的首字母大写)则返回 True |
string.islower() | 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True |
string.isupper() | 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True |
string.islower() This method checks if all the characters of a string are lowercase characters (a-z). 只检查字符串中的英文字母,如果有其它数字或字符不受影响。 string.isupper() 与它类似。
1 | In [51]: test = "adh13()12#" |
string.isspace() 判断字符串是否只包含 whitespaces(包含:空格, 制表符 换行符, 回车
1 | In [63]: test_str="\t\n\r " |
string.isdecimal string.isdigit string.isnumeric
这三种都不能判断小数。开发中由于常常只针对整型数字,因此常用
isdecimal
1 | In [65]: num_str = "一千零一" |
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 中的大小写 |
upper() 和 lower()
大小写转换
1 | 'hello' a= |
string.capitalize() 与 string.title() 区别
string.capitalize() capitalize() method converts the first character of a string to capital (uppercase) letter. 大写的是整个字符串的第一个字符。 如果字符串第一个字符不是英文字母或者是大写的英文字母,那直接返回原字符串的一份 copy
string.title() 就像文章的 title 一样,大写的是字符串中的每一个 word, 并且会把 number 或 symbol 后面的第一个字符也大写。 The title() method returns a string where the first character in every word is upper case. Like a header, or a title.
If the word contains a number or a symbol, the first letter after that will be converted to upper case.
题目: Capitalize!
- capitalize 和 title 方法的区别
- python 还有一个 string 模块有个 string.capwords 方法可以直接实现题目的要求。
- 写代码要尽可能精确,比如: 本题目说 word 之间是 spaces, 而不一定是一个 space, 因此,在 split 的时候,使用 split(" ") 保留空格,比直接 split() 更加符合要求。
4) 文本对齐 - 3
方法 | 说明 |
---|---|
string.ljust(width) | 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串 |
string.rjust(width) | 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串 |
string.center(width) | 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 |
center
Syntax
string.center(length, character)
Parameter Values
Parameter Description length Required. The length of the returned string character Optional. The character to fill the missing space on each side. Default is " " (space)
1 | >>> print(' Welcome to InfoGath '.center(35,'*')) |
6) 拆分和连接 - 5
方法 | 说明 |
---|---|
string.partition(str) | 把字符串 string 分成一个 3 元素的元组 (str 前面, str, str 后面) |
string.rpartition(str) | 类似于 partition() 方法,不过是从右边开始查找 |
string.split(str="", num) | 以 str 为分隔符拆分 string,如果 num 有指定值,则仅分隔 num + 1 个子字符串,str 默认包含 ', ', '' 和空格 |
string.splitlines() | 按照行(', '', '')分隔,返回一个包含各行作为元素的列表 |
string.join(seq) | 以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串 |
string.split(str="", num)
1 | 'I love python' c= |
string.join(seq)
常常用来连接元素为字符串的列表进行固定格式的输出, 示例:
1 | In [54]: str_list = ["hello", "world"] |
0) 统计 - 2
len(string) 统计字符串的长度
1 | In [61]: str_test = "hellow" |
stirng.count(substr)
Python count()
方法用于统计字符串里某个字符或者字符串出现的次数。可选参数为在字符串搜索的开始与结束位置。
如果要查找的字符或字符串在源字符串中没有,程序并不会报错,只会得到结果
0。 str.count(sub, start= 0,end=len(string))
sub -- 搜索的子字符串 返回值 该方法返回子字符串在字符串中出现的次数。
注: 当字符串较长时,counter 函数耗时,慎用
举例:a 字符串中从 索引为 7 开始,a 的个数为 2 个。
1 | In [5]: a="nationl daya!" |
字符串的切片
- 切片 方法适用于
字符串、列表、元组
- 切片 使用 索引值 来限定范围,从一个大的 字符串 中 切出 小的 字符串
- 列表 和 元组 都是 有序 的集合,都能够 通过索引值 获取到对应的数据
- 字典 是一个 无序 的集合,是使用 键值对 保存数据
1 | 字符串[开始索引:结束索引:步长] |
注意:
- 指定的区间属于 左闭右开 型
[开始索引, 结束索引)
=>开始索引 >= 范围 < 结束索引
- 从
起始
位开始,到 **结束
位的前一位** 结束(不包含结束位本身)
- 从
- 不写开始索引。默认从头开始,开始索引 数字可以省略,冒号不能省略
- 不写结束索引,默认到末尾结束,结束索引 数字可以省略,冒号不能省略
- 步长默认为
1
,如果连续切片,数字和冒号都可以省略
索引的顺序和倒序
- 在 Python 中不仅支持 顺序索引,同时还支持 倒序索引
- 所谓倒序索引就是 从右向左 计算索引
- 最右边的索引值是 -1,依次递减
演练需求
- 截取从 2 ~ 5 位置 的字符串
- 截取从 2 ~
末尾
的字符串
- 截取从 2 ~
- 截取从
开始
~ 5 位置 的字符串
- 截取从
- 截取完整的字符串
- 从开始位置,每隔一个字符截取字符串
- 从索引 1 开始,每隔一个取一个
- 截取从 2 ~
末尾 - 1
的字符串
- 截取从 2 ~
- 截取字符串末尾两个字符
- 字符串的逆序(面试题)
答案
1 | num_str = "0123456789" |