当前位置: 首页 > news >正文

python学习笔记—17—数据容器之字符串

1. 字符串

(1) 字符串能通过下标索引来获取其中的元素

(2) 旧字符串无法修改特定下标的元素

(3) index——查找字符串中任意元素在整个字符串中的起始位置(单个字符或字符串都可以)

tmp_str = "supercarrydoinb"
tmp_position1 = tmp_str.index("s")
tmp_position2 = tmp_str.index("doinb")
print(f"{tmp_position1}, {tmp_position2}")

(4) replace——将原字符串中部分字符串1改为字符串2,生成一个新的字符串,不修改原字符串

tmp_str = "supercarrydoinb"
tmp_str1 = tmp_str.replace("doinb", "666")
print(f"{tmp_str1}")

(5) split——将字符串通过字符串中的子分割字符串分割成多个字符串存储到列表中,返回列表名

tmp_str = "super carry doinb"
tmp_str1 = tmp_str.split(" ")
print(f"{tmp_str1}")

(6) strip

        1.   字符串.strip()

                去除字符串前后的空格或回车符,将结果返回给新的字符串

tmp_str = (" super carry doinb ")
tmp_str1 = tmp_str.strip()
print(f"{tmp_str1}")

        2. 字符串.strip(字符串1)

                去除字符串前后的字符串1,将结果返回给新的字符串

tmp_str = ("qwesuper carry doinbewq")
tmp_str1 = tmp_str.strip("qwe")
print(f"{tmp_str1}")

        注意:对于strip中加参数,比如参数是ab,是分别去除a和b,而不是直接去除ab,非整体去除

(7) count——计算字符串中某个字符或部分字符串在整个字符串中的个数

tmp_str = (" super carry doinb ")
tmp_cnt = tmp_str.count("do")
print(f"{tmp_cnt}")

(8) len——计算字符串中字符的个数

tmp_str = ("supercarrydoinb")
tmp_len = len(tmp_str)
print(f"{tmp_len}")

(9) while循环 

tmp_str = ("supercarrydoinb")
tmp_cnt = 0
while tmp_cnt < len(tmp_str):print(f"{tmp_str[tmp_cnt]}")tmp_cnt += 1

(10) for循环

tmp_str = ("supercarrydoinb")
for i in tmp_str:print(f"{i}")

(11) 字符串特点

        字符串只能存储字符类型,且不可被修改

(12) 练习

tmp_str = ("super carry doinb")
tmp_count = tmp_str.count("super")
print(f"{tmp_count}")
tmp_str1 = tmp_str.replace(" ", "|")
print(f"{tmp_str1}")
tmp_str2 = tmp_str1.split("|")
print(f"{tmp_str2}")

        

http://www.lryc.cn/news/518728.html

相关文章:

  • UE5 使用内置组件进行网格切割
  • 51单片机——串口通信(重点)
  • Taro+Vue实现图片裁剪组件
  • PHP民宿酒店预订系统小程序源码
  • Hadoop3.x 万字解析,从入门到剖析源码
  • VUE3 常用的组件介绍
  • deepin-Wine 运行器合并打包器和添加从镜像提取 DLL 的功能
  • [大模型]本地离线运行openwebui+ollama容器化部署
  • 再次梳理ISP的大致流程
  • HBuilderX打包ios保姆式教程
  • 《解锁鸿蒙系统AI能力,开启智能应用开发新时代》
  • rhcsa练习(3)
  • 科研绘图系列:R语言绘制Y轴截断分组柱状图(y-axis break bar plot)
  • 跳出技术陷阱,探索财富自由的多元路径
  • qml SpringAnimation详解
  • 中学综合素质笔记3
  • uniapp vue2版本如何设置i18n
  • 【踩坑记录❌】ubuntu 安装 NVIDIA 显卡驱动不要 autoinstall
  • vue3 + ts + element-plus(el-upload + vuedraggable实现上传OSS并排序)
  • SQL开窗函数相关的面试题和答案
  • 【数据分析(一)】初探 Numpy
  • 国产化ARM平台-飞腾派开发板硬件与系统
  • [ LeetCode 75 ] 283 移动零(JavaScript)
  • 前端学习汇总
  • 蓝笔科技 | 超凡妈妈赋能计划-【北大生涯规划师特别企划】
  • 【电路笔记 TMS320C6***DSP】C6748 EDMA3配置笔记 寄存器配置+影子通道寄存器+配置示例
  • 2025新春烟花代码(二)HTML5实现孔明灯和烟花效果
  • apollo内置eureka dashboard授权登录
  • 后台管理系统全屏功能实现
  • 风电叶片市场竞争激烈:开启绿色能源新篇章的巨大潜力