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

python之字符串

创建字符串

s = "Hello, World!"

常用字符串操作

  1. 获取字符串长度
length = len(s)
print(length)  # 输出: 13
  1. 字符串拼接
s1 = "Hello"
s2 = "World"
s3 = s1 + ", " + s2 + "!"
print(s3)  # 输出: Hello, World!
  1. 重复字符串
s = "Hello"
s_repeated = s * 3
print(s_repeated)  # 输出: HelloHelloHello
  1. 字符串格式化
name = "Alice"
age = 30
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string)  # 输出: My name is Alice and I am 30 years old.
  1. 字符串分割
s = "Hello, World!"
parts = s.split(", ")
print(parts)  # 输出: ['Hello', 'World!']
  1. 字符串连接
words = ["Hello", "World"]
joined_string = " ".join(words)
print(joined_string)  # 输出: Hello World
  1. 字符串替换
s = "Hello, World!"
new_s = s.replace("World", "Python")
print(new_s)  # 输出: Hello, Python!
  1. 字符串查找
s = "Hello, World!"
index = s.find("World")
print(index)  # 输出: 7
  1. 字符串大小写转换
s = "Hello, World!"
print(s.upper())  # 输出: HELLO, WORLD!
print(s.lower())  # 输出: hello, world!
print(s.capitalize())  # 输出: Hello, world!
print(s.title())  # 输出: Hello, World!
  1. 去除空白字符
s = "  Hello, World!  "
print(s.strip())  # 输出: Hello, World!
print(s.lstrip())  # 输出: Hello, World!  
print(s.rstrip())  # 输出:   Hello, World!

子字符串的用法

  1. 获取子字符串
s = "Hello, World!"
sub_s = s[7:12]
print(sub_s)  # 输出: World
  1. 检查子字符串
s = "Hello, World!"
print("World" in s)  # 输出: True
print("Python" in s)  # 输出: False
  1. 子字符串计数
s = "Hello, World! Hello, Python!"
count = s.count("Hello")
print(count)  # 输出: 2
  1. 子字符串索引
s = "Hello, World!"
index = s.index("World")
print(index)  # 输出: 7# 如果子字符串不存在,会抛出 ValueError
try:index = s.index("Python")
except ValueError:print("Substring not found")
  1. 子字符串切片
s = "Hello, World!"
sub_s = s[7:]  # 从索引 7 开始到结尾
print(sub_s)  # 输出: World!sub_s = s[:5]  # 从开头到索引 5(不包括 5)
print(sub_s)  # 输出: Hellosub_s = s[::2]  # 每隔一个字符取一个
print(sub_s)  # 输出: Hlo ol!

str.find()与str.index()

使用 str.find() 方法查找子字符串时,如果子字符串不存在于原字符串中,find() 方法不会报错,而是返回 -1str.index()在找不到子字符串时会抛出 ValueError 异常。

str.find()

s = "Hello, World!"
index = s.find("World")
print(index)  # 输出: 7# 查找不存在的子字符串
index = s.find("Python")
print(index)  # 输出: -1

str.index()

s = "Hello, World!"
index = s.index("World")
print(index)  # 输出: 7# 查找不存在的子字符串,会抛出 ValueError
try:index = s.index("Python")
except ValueError:print("Substring not found")  # 输出: Substring not found
  • str.find(substring):返回子字符串在原字符串中的最低索引,如果找不到则返回 -1
  • str.index(substring):返回子字符串在原字符串中的最低索引,如果找不到则抛出 ValueError
http://www.lryc.cn/news/432887.html

相关文章:

  • 算法打卡 Day28(回溯算法)-组合总数 + 组合总数 Ⅱ+ 电话号码的字母组合
  • 【Hadoop|MapReduce篇】MapReduce概述
  • 设置Virtualbox虚拟机共享文件夹
  • 从零开始的机器学习之旅
  • 开源还是封闭?人工智能的两难选择
  • Prometheus 服务监控
  • 建模杂谈系列252 规则的串行改并行
  • 0.ffmpeg面向对象oopc
  • KDD2024参会笔记-Day1
  • Java操作Elasticsearch的实用指南
  • 数据库系统 第42节 数据库索引简介
  • C++11 --- 智能指针
  • C#顺序万年历自写的求余函数与周位移算法
  • 【Java并发编程一】八千字详解多线程
  • CentOS 8FTP服务器
  • C++ | Leetcode C++题解之第385题迷你语法分析器
  • 【软件设计师真题】第一大题---数据流图设计
  • 系统架构的发展历程之模块化与组件化
  • 基因组学中的深度学习
  • 解决老师询问最高分数问题的编程方案
  • com.baomidou.mybatisplus.annotation.DbType 无法引入
  • 从零开始学习JVM(七)- StringTable字符串常量池
  • 数据库课程设计mysql
  • AI学习指南深度学习篇-带动量的随机梯度下降法的基本原理
  • 点餐小程序实战教程03创建应用
  • 鸿蒙自动化发布测试版本app
  • 力扣9.7
  • GPU 带宽功耗优化
  • Linux Centos 7网络配置
  • 第三天旅游线路规划