python学习26:python中的字符串的定义和操作

python中的字符串的定义和操作

1.和其他容器:列表、元组一样,字符串也可以通过下标进行访问

  • 从前向后,下标从0开始
  • 从后向前,下标从-1开始
python 复制代码
#通过下标获取特定位置字符
name='coco'
print(name[0])  #结果是c
print(bane[1])  #结果是o

字符串同元组一样,是一个无法修改的数据容器

python 复制代码
my_str = "coco and xuanxuan"
# my_str[1] = 'h'  # 报错  'str' object does not support item assignment

2.字符串的常规操作

  1. index方法,判断字符串中某个字符出现的第一个位置
python 复制代码
# index方法  判断第一个字符出现的的位置
my_str = "coco and xuanxuan"
index = my_str.index('o')
index1 = my_str.index('coco')
index2 = my_str.index('cococo')
print(f'字符o在字符串中的位置是:{index}')  # 字符o在字符串中的位置是:1
print(f'字符coco在字符串中的位置是:{index1}')  # 字符coco在字符串中的位置是:0
print(f'字符coco在字符串中的位置是:{index2}')  # 这个就会报错:ValueError: substring not found

2.replace 方法是字符换中的替换

python 复制代码
# replace方法
"""
字符串的替换:
语法:字符串.replace(字符串1,字符串2)
功能:将字符串内的全部:字符串1,替换为字符串2
注意:不是修改字符串本身,而是得到了一个新字符串
"""
my_str = "hello and world"
new_str = my_str.replace('hello', '你好')
# 结果是:字符串:hello and world经过replace操作后变为:你好 and world
print(f'字符串:{my_str}经过replace操作后变为:{new_str}')

3.split方法,字符串的分割;语法:字符串.split(分割符字符串),默认是以空格来分割的

python 复制代码
"""
字符串的分割
语法:字符串.split(分割符字符串),默认是以空格来分割的
功能:按照指定的分隔符字符串,将字符串划分为多个字符串,并存入列表对象中
注意:字符串本身不变,而得到了一个列表对象
"""
my_str = "hello and world"
res = my_str.split()
# 字符串hello and world经过split后变为['hello', 'and', 'world'],
print(f'字符串{my_str}经过split后变为{res}')

4.strip方法,去除前后空格,或者前后指定字符串的(中间的不会处理的)

python 复制代码
# strip方法
"""
1.语法:字符串.strip()  去前后空格
2.语法:字符串.strip(字符串) 去前后指定字符串
"""
# 1。去前后空格
my_str = "   hello  and  world   "
new_str = my_str.strip()
#  结果是:字符串:   hello  and  world   经过strip变为:hello  and  world
print(f'字符串:{my_str}经过strip变为:{new_str}')

# 2.去前后指定字符串
my_str1 = "12hello and world21"
new_str1 = my_str1.strip('12')
# 结果是:字符串:12hello and world21经过strip变为:hello and world
print(f'字符串:{my_str1}经过strip变为:{new_str1}')
# 注意传入的是'12',其实是"1"和"2"都会移除,是按照单个字符串移除的

5.count 统计字符串中某字符串出现的次数

python 复制代码
"""
count 同列表和元组是一样的方法来统计出现的次数
"""
my_str = "hello and world"
count = my_str.count('l')
# 结果是:字符串hello and world中出现字符l的次数是:3
print(f'字符串{my_str}中出现字符l的次数是:{count}')

6.len:计字符串的长度

python 复制代码
# 统计字符串的长度
my_str = "hello world"
lenstr = len(my_str)
# 结果是:字符串hello world的长度是11
print(f'字符串{my_str}的长度是{lenstr}')

3.字符串的循环(使用while或者for)

python 复制代码
# 字符串的循环
my_str = "世界那么大,我想去看看"
index = 0
while index < len(my_str):
    print(f'字符串中的第{index + 1}个字符是:{my_str[index]}')
    index += 1

# for循环
index = 1
for i in my_str:
    print(f'字符串中的第{index}个字符是:{i}')
    index += 1

"""
上面两个循环的结果是一样的
结果如下:
字符串中的第1个字符是:世
字符串中的第2个字符是:界
字符串中的第3个字符是:那
字符串中的第4个字符是:么
字符串中的第5个字符是:大
字符串中的第6个字符是:,
字符串中的第7个字符是:我
字符串中的第8个字符是:想
字符串中的第9个字符是:去
字符串中的第10个字符是:看
字符串中的第11个字符是:看
"""
相关推荐
IVEN_5 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang6 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮6 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling6 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮9 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽10 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers