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个字符是:看
"""
相关推荐
天天爱吃肉82185 分钟前
商用车多体动力学实战笔记|第6篇:动力传动系统(发动机、变速箱、分动器、TCS、LSD限滑差速)
大数据·人工智能·笔记·python·嵌入式硬件·汽车
海盗123424 分钟前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
大数据魔法师24 分钟前
【最全详解】DuckDB Python 全套 API 语法、参数与实战用法(零基础全覆盖)
python·数据分析
MC皮蛋侠客2 小时前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
Zane19942 小时前
@property 到底是怎么把方法伪装成属性的?一文吃透 property、staticmethod、classmethod
后端·python
qq_316411032 小时前
AI 情感陪伴智能潮玩软硬件一体化开发案例
人工智能·python
废弃的小码农2 小时前
功能测试--Day07--Python编程基础
开发语言·python
zx1154503 小时前
大模型工具调用次数限制
人工智能·python
MC皮蛋侠客3 小时前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
大数据魔法师4 小时前
Python 网络请求库 curl_cffi:从入门到实战,如何规避网站指纹检测
python·数据分析