Python初学笔记之字符串

一、字符串的定义

字符串是就一堆字符,可以使用""(双引号)、‘’(单引号)来创建。

1 one_str = "定义字符串"

字符串内容中包含引号时,可以使用转义字符:/'、/"。

二、字符串的常见操作

1.索引的定义

  

1 #使用索引打印y
2 one_str = "My name is apple!"
3 print(one_str[1])

2.字符串的切片

object[start_index:end_index:step]

start_index:

    • 起始索引;
    • 包含本身;
    • 省略时,表示从对象“端点”开始取值;

end_index:

    • 结束索引
    • 不包含本身
    • 省略时,表示取到对象的“端点”

step:

    •  步长,可正可负
    • 正数:从左向右取
    • 负数:从右向左取

1 one_str = "字符串的切片操作示例"
2 print(one_str[::])      # 三个参数都可以省略。省略时,默认从起始正向打印至结束,步长为1
3 print(one_str[::1])     # 正向打印 默认步长1的时候,:1可以省略
4 print(one_str[::-1])    # 反向打印
5 print(one_str[1:4])     # 左闭右开的原则
6 print(one_str[::2])     # 正向,步长为3打印,即一个间隔一个地打印
7 print(one_str[::-2])     # 反向,步长为3打印
8 print(one_str[2:-2:2])      # 可以正向索引、反向索引结合使用

上面代码执行结果:

字符串的切片操作示例
字符串的切片操作示例
例示作操片切的串符字
符串的
字串切操示
例作片的符
串切操

切片的组合是花式多样的,只要符合切片的规则,这里不一一列举了。

切片适用于:字符串、列表、元祖等序列类型

3、字符串的内置方法

3.1判断类型

方法          什么情况下是TRUE
one_str.isalnum() # 字符串全部为字母或者数字
one_str.isdigit() # 字符串全部为数字
one_str.isalpha() # 字符串全部为字母

one_str.isspace() # 只包含空格

one_str.isupper() # 全部为大写
one_str.islower() # 全部为小写
one_str.istitle() # 每个单词首字母都是大写

3.2查找替换

one_str.startswith(str)     #判断是否以str开始
one_str.endswith(str) #判断是否以str结尾
one_str.find(str,start,end) # 从start至end范围内,是否存在str,存在返回索引,不存在返回-1
one_str.index(str,start,end)
one_str.replace(old_str,new_str,num) # 讲字符串中的old_str换成new_str,若num指定,则表示替换num次
1 one_str = "It's a beautiful day,isn't it?1234567"
2 print(one_str.startswith("It's"))
3 print(one_str.endswith("67"))
4 print(one_str.find("day", 0, len(one_str)))
5 print(one_str.rfind("456", 0, len(one_str)))        # 与find类似,但是从右向左查询
6 print(one_str.find("happy", 0, len(one_str)))       # 不存在的情况下,报-1
7 print(one_str.index("day", 0, len(one_str)))      # 与find类似,但不存在的情况下,报错
8 print(one_str.index("day", 0, len(one_str)))      # 与index类似,但是从右向左查询
9 print(one_str.replace("beautiful", "nice", 1))

结果:

True
True
17
33
-1
17
17
It's a nice day,isn't it?1234567

3.3大小写替换

one_str.lower()     # 转化为小写
one_str.upper() # 转化为大写
one_str.title() # 每个单词首字母大写
one_str.capitalize() # 字符串首字母大写
one_str.swapcase() # 翻转大小写
1 one_str = "it's a beautiful DAY,isn't it?"
2 print(one_str.lower())
3 print(one_str.upper())
4 print(one_str.title())
5 print(one_str.capitalize())
6 print(one_str.swapcase())

结果:

it's a beautiful day,isn't it?
IT'S A BEAUTIFUL DAY,ISN'T IT?
It'S A Beautiful Day,Isn'T It?
It's a beautiful day,isn't it?
IT'S A BEAUTIFUL day,ISN'T IT?

3.4文本对齐

3.5去除空白字符

  删除左侧、右侧、两侧的空白字符

1 one_str = "   Python*字*符*串*操*作练习*!!   *示例  "
2 print(one_str.lstrip())
3 print(one_str.rstrip())
4 print(one_str.strip())

结果:

Python*字*符*串*操*作练习*!!   *示例  
   Python*字*符*串*操*作练习*!!   *示例
Python*字*符*串*操*作练习*!!   *示例

3.6拆分和连接

one_str.partition(str)      # 返回类型为【元祖】。以&作为分隔,将字符串分为:str左侧,str,str右侧三部分
one_str.rpartition(str) # 与partition类似,但是从右侧查找str,并切分
1 one_str = "好好学习, 天天向上&Python初学笔记&字符串&列表"
2 print(one_str.partition("&"))
3 print(one_str.rpartition("&"))

结果:

('好好学习, 天天向上', '&', 'Python初学笔记&字符串&列表')
('好好学习, 天天向上&Python初学笔记&字符串', '&', '列表')
one_str.split(str,num)        # 以str为标志进行切分,切分结果为【list】类型。如果定义了num,则代表切分num次
one_str = "好好学习, 天天向上&Python初学笔记&字符串&列表"
print(one_str.split("&",1))
print(one_str.split("&",2))
print(one_str.split("&"))

结果:

['好好学习, 天天向上', 'Python初学笔记&字符串&列表']
['好好学习, 天天向上', 'Python初学笔记', '字符串&列表']
['好好学习, 天天向上', 'Python初学笔记', '字符串', '列表']
str.join(one_list)      # 以str连接one_list的所有元素
1 one_str = "好好学习&天天向上&Python初学笔记&字符串&列表"
2 one_list = one_str.split("&")
3 print(one_list)
4 print("+++".join(one_list))

结果:

['好好学习', '天天向上', 'Python初学笔记', '字符串', '列表']
好好学习+++天天向上+++Python初学笔记+++字符串+++列表

猜你喜欢

转载自www.cnblogs.com/jszfy/p/11066562.html