Python字符串切片、查找、修改、删除、判断等常用操作
发布日期:2021-05-15 00:32:58 浏览次数:23 分类:精选文章

本文共 3030 字,大约阅读时间需要 10 分钟。

Python 字符串操作指南

1. 切片

切片是对字符串操作对象截取一部分的操作,适用于字符串、列表和元组等可切片的对象。

1.1 字符串切片

name = "abcdefg"print(name[2:5])  # 输出: "cde"print(name[:5])   # 输出: "abcde"print(name[1:])   # 输出: "bcdefg"print(name[::-1])  # 输出: "gfedcba"

1.2 逆序字符串

name = "abcdefg"print(name[::-1])  # 输出: "gfedcba"

2. 查找

2.1 find() 方法

检测子串是否包含在字符串中,返回子串起始位置或 -1。

mystr = "hello world and itcast and itheima and Python"print(mystr.find('and'))  # 输出: 12print(mystr.find('ands'))  # 输出: -1

2.2 index() 方法

检测子串是否存在,存在则返回起始位置,不存在则报异常。

mystr = "hello world and itcast and itheima and Python"print(mystr.index('and'))  # 输出: 12

2.3 count() 方法

返回子串在字符串中出现的次数。

mystr = "hello world and itcast and itheima and Python"print(mystr.count('and'))  # 输出: 3

2.4 rfind()rindex()

从字符串末尾开始查找子串。

mystr = "hello world and itcast and itheima and Python"print(mystr.rfind('and'))  # 输出: 23

3. 修改

3.1 replace() 方法

替换字符串中的子串。

mystr = "hello world and itcast and itheima and Python"print(mystr.replace('and', 'he'))  # 输出: "hello world he itcast he itheima he Python"

3.2 split() 方法

分割字符串,返回列表。

mystr = "hello world and itcast and itheima and Python"print(mystr.split('and'))  # 输出: ['hello world ', ' itcast ', ' itheima ', ' Python']

3.3 join() 方法

合并多个字符串。

list1 = ['chuan', 'zhi', 'bo', 'ke']print('_'.join(list1))  # 输出: "chuan_zhi_bo_ke"

3.4 capitalize() 方法

将字符串的第一个字符转换为大写,其余字符转换为小写。

mystr = "hello world and itcast and itheima and Python"print(mystr.capitalize())  # 输出: "Hello world and itcast and itheima and python"

3.5 title() 方法

将字符串中的每个单词首字母转换为大写。

mystr = "hello world and itcast and itheima and Python"print(mystr.title())  # 输出: "Hello World And Itcast And Itheima And Python"

3.6 lower() 方法

将字符串中的大写字符转换为小写。

mystr = "hello world and itcast and itheima and Python"print(mystr.lower())  # 输出: "hello world and itcast and itheima and python"

3.7 upper() 方法

将字符串中的小写字符转换为大写。

mystr = "hello world and itcast and itheima and Python"print(mystr.upper())  # 输出: "HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON"

4. 删除

4.1 删除空白字符

mystr = "    hello world    "print(mystr.lstrip())  # 输出: "hello world    "print(mystr.rstrip())  # 输出: "    hello world"print(mystr.strip())  # 输出: "hello world"

4.2 字符串对齐

mystr = "hello world"print(mystr.ljust(20))  # 输出: "hello world              "print(mystr.rjust(20))  # 输出: "               hello world"print(mystr.center(20))  # 输出: "         hello world        "

5. 判断

5.1 startswith() 方法

检查字符串是否以指定子串开头。

mystr = "hello world and itcast and itheima and Python"print(mystr.startswith('hello'))  # 输出: True

5.2 endswith() 方法

检查字符串是否以指定子串结尾。

mystr = "hello world and itcast and itheima and Python"print(mystr.endswith('Python'))  # 输出: True

5.3 isalpha() 方法

检查字符串是否全为字母。

mystr1 = 'hello'  # 输出: Truemystr2 = 'hello12345'  # 输出: False

5.4 isdigit() 方法

检查字符串是否全为数字。

mystr1 = 'aaa12345'  # 输出: Falsemystr2 = '12345'  # 输出: True

5.5 isalnum() 方法

检查字符串是否全为字母或数字。

mystr1 = 'aaa12345'  # 输出: Truemystr2 = '12345-'  # 输出: False

5.6 isspace() 方法

检查字符串是否全为空白字符。

mystr1 = '1 2 3 4 5'  # 输出: Falsemystr2 = ' '  # 输出: True

以上方法是对字符串操作的基础实现,熟练掌握这些方法能够显著提升代码效率和代码质量。

上一篇:Python字典和集合删除、修改、查找等常见操作
下一篇:C学习笔记——(6)系统性的介绍C语言中的指针操作

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年05月02日 03时55分51秒