全网最细,python中关于字符串常用方法的总结与说明 举例说明 附源码可直接运行

Python中关于字符串的方法非常多,下面列举一些常用的方法:

字符串基本操作:

  • len(str):返回字符串str的长度。
  • str[index]:返回字符串str中索引为index的字符。
  • str[start:end]:返回字符串str中从索引start到end-1的子串。
  • str.find(sub):返回字符串str中第一次出现子串sub的位置。如果没有找到,返回-1。
  • str.replace(old, new):将字符串str中的所有old子串替换成new子串。
# 字符串基本操作:

# len(str): 返回字符串str的长度。
str1 = "Hello, World!"
print(len(str1))  # 输出 13

# str[index]: 返回字符串str中索引为index的字符。
str2 = "Hello, World!"
print(str2[0])  # 输出 'H'

# str[start:end]: 返回字符串str中从索引start到end-1的子串。
str3 = "Hello, World!"
print(str3[7:12])  # 输出 'World'

# str.find(sub): 返回字符串str中第一次出现子串sub的位置。如果没有找到,返回-1。
str4 = "Hello, World!"
print(str4.find("o"))  # 输出 4
print(str4.find("Python"))  # 输出 -1

# str.replace(old, new): 将字符串str中的所有old子串替换成new子串。
str5 = "Hello, World!"
new_str = str5.replace("World", "Python")
print(new_str)  # 输出 'Hello, Python!'

 运行结果如下:

字符串转换:

  • str.upper():返回字符串str的大写形式。
  • str.lower():返回字符串str的小写形式。
  • str.capitalize():返回字符串str的首字母大写形式。
  • str.title():返回字符串str的单词首字母大写形式。
  • str.strip():返回字符串str去除两侧空格的形式。

 

# 字符串转换:

# str.upper(): 返回字符串str的大写形式。
str1 = "Hello, World!"
print(str1.upper())  # 输出 'HELLO, WORLD!'

# str.lower(): 返回字符串str的小写形式。
str1 = "Hello, World!"
print(str1.lower())  # 输出 'hello, world!'

# str.capitalize(): 返回字符串str的首字母大写形式。
str1 = "hello, world!"
print(str1.capitalize())  # 输出 'Hello, world!'

# str.title(): 返回字符串str的单词首字母大写形式。
str1 = "hello, world!"
print(str1.title())  # 输出 'Hello, World!'

# str.strip(): 返回字符串str去除两侧空格的形式。
str1 = "   Hello, World!   "
print(str1.strip())  # 输出 'Hello, World!'

 结果如下:

字符串分割与连接:

  • str.split(sep):使用sep作为分隔符,将字符串str分割成一个列表。
  • str.join(iterable):将可迭代对象iterable中的元素用字符串str连接成一个新的字符串。
# 字符串分割与连接:

# str.split(sep): 使用sep作为分隔符,将字符串str分割成一个列表。
str1 = "apple,banana,orange"
lst = str1.split(",")
print(lst)  # 输出 ['apple', 'banana', 'orange']

# str.join(iterable): 将可迭代对象iterable中的元素用字符串str连接成一个新的字符串。
lst = ['apple', 'banana', 'orange']
str2 = ",".join(lst)
print(str2)  # 输出 'apple,banana,orange'

 结果如下:

字符串判断:

  • str.startswith(prefix):判断字符串str是否以“prefix”开头。
  • str.endswith(suffix):判断字符串str是否以”suffix“结尾。
  • str.isalpha():判断字符串str是否全由字母组成。
  • str.isdigit():判断字符串str是否全由数字组成。
# 字符串判断:

# str.startswith(prefix): 判断字符串str是否以prefix开头。
str = "Hello, World!"
print(str.startswith("Hello"))  # 输出 True
print(str.startswith("Python"))  # 输出 False

# str.endswith(suffix): 判断字符串str是否以suffix结尾。
str = "Hello, World!"
print(str.endswith("World!"))  # 输出 True
print(str.endswith("Python!"))  # 输出 False

# str.isalpha(): 判断字符串str是否全由字母组成。
str = "Hello"
print(str.isalpha())  # 输出 True
str = "Hello123"
print(str.isalpha())  # 输出 False

# str.isdigit(): 判断字符串str是否全由数字组成。
str = "123"
print(str.isdigit())  # 输出 True
str = "Hello123"
print(str.isdigit())  # 输出 False

 结果如下:

 如果这篇文章对你有帮助,还请帮忙点赞关注。您的支持是我更新的最大动力!

                                         

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 19:10:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 19:10:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 19:10:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 19:10:01       18 阅读

热门阅读

  1. 盘点好用内容合规监测工具

    2024-01-22 19:10:01       72 阅读
  2. leetcode-二叉树的后序遍历

    2024-01-22 19:10:01       43 阅读
  3. 数据结构 | 数组

    2024-01-22 19:10:01       38 阅读
  4. 大模型镜像打包实战:CodeGeeX2为例

    2024-01-22 19:10:01       40 阅读
  5. ansible-设置互信

    2024-01-22 19:10:01       26 阅读