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个字符是:看
"""
相关推荐
PythonFun2 小时前
Python批量下载PPT模块并实现自动解压
开发语言·python·powerpoint
炼丹师小米2 小时前
Ubuntu24.04.1系统下VideoMamba环境配置
python·环境配置·videomamba
GFCGUO2 小时前
ubuntu18.04运行OpenPCDet出现的问题
linux·python·学习·ubuntu·conda·pip
985小水博一枚呀4 小时前
【深度学习基础模型】神经图灵机(Neural Turing Machines, NTM)详细理解并附实现代码。
人工智能·python·rnn·深度学习·lstm·ntm
萧鼎5 小时前
Python调试技巧:高效定位与修复问题
服务器·开发语言·python
IFTICing6 小时前
【文献阅读】Attention Bottlenecks for Multimodal Fusion
人工智能·pytorch·python·神经网络·学习·模态融合
大神薯条老师6 小时前
Python从入门到高手4.3节-掌握跳转控制语句
后端·爬虫·python·深度学习·机器学习·数据分析
程序员爱德华6 小时前
Python环境安装教程
python
huanxiangcoco6 小时前
152. 乘积最大子数组
python·leetcode
萧鼎6 小时前
Python常见问题解答:从基础到进阶
开发语言·python·ajax