【Python百日精通】Python 字符串的常用操作方法

文章目录

  • 引言
  • 一、查找
    • 1.1 find() 方法
    • 1.2 index() 方法
    • 1.3 rfind() 和 rindex() 方法
    • 1.4 count() 方法
  • 二、修改
    • 2.1 replace() 方法
    • 2.2 split() 方法
    • 2.3 join() 方法
  • 三、判断
    • 3.1 startswith() 方法
    • 3.2 endswith() 方法
    • 3.3 isalpha() 方法
    • 3.4 isdigit() 方法
    • 3.5 isalnum() 方法
    • 3.6 isspace() 方法
  • 四、总结

引言

在 Python 中,字符串不仅可以用来存储和操作文本数据,还有许多强大的内置方法用于处理字符串。本文将详细介绍 Python 字符串的常用操作方法,包括查找、修改和判断方法,通过实际示例帮助你掌握这些操作。

一、查找

1.1 find() 方法

find() 方法用于查找子串在字符串中的位置。它的语法是 string.find(substring, start, end),其中 substring 是要查找的子串,startend 是可选参数,表示查找的范围。如果子串存在,返回子串的起始位置下标,否则返回 -1。例如:

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

1.2 index() 方法

index() 方法与 find() 方法类似,但如果子串不存在,它会抛出一个 ValueError 异常。例如:

print(mystr.index('and'))         # 输出 12
print(mystr.index('and', 15, 30)) # 输出 23
# print(mystr.index('ands'))     # 报错:ValueError: substring not found

1.3 rfind() 和 rindex() 方法

rfind()rindex() 方法分别从右侧开始查找子串的位置,返回最后一次出现的下标。例如:

print(mystr.rfind('and'))         # 输出 35
print(mystr.rindex('and'))        # 输出 35

1.4 count() 方法

count() 方法返回子串在字符串中出现的次数。例如:

print(mystr.count('and'))         # 输出 3
print(mystr.count('ands'))        # 输出 0

二、修改

2.1 replace() 方法

replace() 方法用于替换字符串中的子串。它的语法是 string.replace(old, new, count),其中 old 是要替换的子串,new 是替换成的新子串,count 是替换的次数。例如:

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

2.2 split() 方法

split() 方法按照指定字符将字符串分割成多个子串。它的语法是 string.split(separator, num),其中 separator 是分割字符,num 是分割的次数。例如:

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

2.3 join() 方法

join() 方法用于将多个字符串合并为一个字符串。它的语法是 separator.join(iterable),其中 separator 是用于分隔的字符,iterable 是一个可迭代对象(如列表、元组)。例如:

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

三、判断

3.1 startswith() 方法

startswith() 方法检查字符串是否以指定的子串开头。如果是,则返回 True,否则返回 False。它的语法是 string.startswith(substring, start, end)。例如:

print(mystr.startswith('hello'))    # 输出 True
print(mystr.startswith('hello', 5, 20)) # 输出 False

3.2 endswith() 方法

endswith() 方法检查字符串是否以指定的子串结尾。如果是,则返回 True,否则返回 False。它的语法是 string.endswith(substring, start, end)。例如:

print(mystr.endswith('Python'))    # 输出 True
print(mystr.endswith('python'))    # 输出 False
print(mystr.endswith('Python', 2, 20)) # 输出 False

3.3 isalpha() 方法

isalpha() 方法检查字符串是否只包含字母。如果是,则返回 True,否则返回 False。例如:

print('hello'.isalpha())     # 输出 True
print('hello12345'.isalpha()) # 输出 False

3.4 isdigit() 方法

isdigit() 方法检查字符串是否只包含数字。如果是,则返回 True,否则返回 False。例如:

print('12345'.isdigit())    # 输出 True
print('12345abc'.isdigit()) # 输出 False

3.5 isalnum() 方法

isalnum() 方法检查字符串是否只包含字母和数字。如果是,则返回 True,否则返回 False。例如:

print('abc123'.isalnum())  # 输出 True
print('abc 123'.isalnum()) # 输出 False

3.6 isspace() 方法

isspace() 方法检查字符串是否只包含空白字符。如果是,则返回 True,否则返回 False。例如:

print('   '.isspace())  # 输出 True
print('abc'.isspace()) # 输出 False

四、总结

掌握字符串的常用操作方法对于高效处理文本数据至关重要。了解如何查找、修改和判断字符串中的内容,将帮助你在编程过程中更好地处理和分析文本数据。字符串处理是编程的核心技能之一,掌握这些方法将大大提升你的编程能力和效率。

【Python百日精通】Python 字符串的常用操作方法_第1张图片

你可能感兴趣的:(精通Python百日计划,python,java,算法)