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个字符是:看
"""
相关推荐
东莞市云毅网络有限公司3 小时前
用 Node 做收录快照的周对比:差集、阈值与告警
python·sqlite·自动化运维·geo·数据监测
山楂树の4 小时前
Python Pandas 数据处理:数据读取、筛选、清洗、排序与分组
开发语言·python·pandas
Logintern094 小时前
langgraph 图编译的聊天机器人
python·langgraph
xurileidian5 小时前
第 22 篇 · 边玩边学Python · 双等号与 is 不是一回事
开发语言·python·spring
钱栈up7 小时前
GitHub热榜实测|Sherlock:一个用户名搜遍全网
python·开源·github
geovindu7 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
Agent世界7 小时前
发布台账字段漂移:1265 条记录里 190 条没有标题
python·自动化运维
用户298698530149 小时前
Python 自动化处理 Word:按规则查找并高亮关键文本
python·api·编程语言
人工智能技术咨询.10 小时前
什么才算高质量数据集
python
DanCheng-studio10 小时前
毕业设计项目 机器视觉指纹识别特征对比算法(源码+论文)
python·毕业设计·毕设