python基础:字符串的相关操作

在这里插入图片描述

hello_str="hello hello"
#1.统计字符串长度
print(len(hello_str))
#2.统计某一个字符串出现的次数
print(hello_str.count("llo"))
print(hello_str.count("abc"))
#3.某一个字符串出现的位置
print(hello_str.index("llo"))
#没有会报错
#1.判断空白字符
space_str="   \t\n\r"
print(space_str.isspace())

在这里插入图片描述

hello_str="hello hello"
#1.判断是否以指定字符串开始
print(hello_str.startswith("hello"))

#2.判断是否以字符串结束
print(hello_str.endswith("hello"))

#3.查找指定字符
#index 可以查找指定字符串在大字符串中的索引
print(hello_str.find("llo"))
#但是find方法如果没找到会返回-1,而index 如果没找到会报错
print(hello_str.find("abc"))
#4.替换字符串,不会改变原有字符串
print(hello_str.replace("hello","python"))
print(hello_str)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#假设,以下内容是从网络上抓取的
#要求:
#1.将字符串中的空白字符全部丢掉
#2.再使用“ ”作为分隔符,拼接成一个整齐的字符串
poem="aaaa\t bbb \t ccccc\t\n ddddd\t\t"
print(poem)
#1.拆分字符串,split会把它拆分成一个列表
poem_list=poem.split()
print(poem_list)
#2.合并字符串,join拼接成一个大字符串
result="".join(poem_list)
print(result)

字符串的切片
1.切片使用索引值来限定范围,从一个大的字符串中切出小的字符串
2.列表和元组都是有序的集合,都能够通过索引值获取到对应的数据
3.字典是一个无序的集合,使用键值对保存数据

字符串[开始索引:结束索引:步长]
切片不包括结束索引
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
字符串逆序:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值