前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python中字符串的一些方法回顾

Python中字符串的一些方法回顾

作者头像
benym
发布2022-07-14 13:47:42
1760
发布2022-07-14 13:47:42
举报
文章被收录于专栏:后端知识体系后端知识体系

# python中字符串的一些方法回顾

# 代码

代码语言:javascript
复制
hello_str = "hello world"

# 1、统计字符串长度
print(len(hello_str))
# 2、统计某一个小字符串出现的次数
print(hello_str.count("llo"))
# 3、某一个子字符串出现的位置
print(hello_str.index("llo"))
# 4、输出数学的平方
num_str = "\u00b2"
print(num_str)
# 5、判断是否以指定字符串开始
print(hello_str.startswith("hello"))
# 6、判断是否以指定字符串结束
print(hello_str.endswith("world"))
# 7、查找指定字符串
# 在index方法中,如果指定的字符串不存在,会直接报错
# 在find方法中,如果指定的字符串不存在,会返回-1
print(hello_str.find("llo"))
print(hello_str.find("CCC"))
# 8、替换字符串
# replace方法执行完成之后会返回一个新的字符串,但是不会修改原有字符串的内容
hh = hello_str.replace("world", "python")
print(hh)
print(hello_str)

# 运行结果

代码语言:javascript
复制
11
1
2
²
True
True
2
-1
hello python
hello world
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # python中字符串的一些方法回顾
  • # 代码
  • # 运行结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档