python字符串常用操作方法

一、查找

1.1 find():检测某个字串是否保护焊在这个字符串中,如果在,返回这个字串开始的位置下标,否则返回-1

  1. 语法
str(seq).find('子串',开始位置下标,结束位置下标)

注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找

  1. demo
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 rfind():从右侧查找

demo

mystr = "Hello world and itcast and itheima and Python"

print(mystr.rfind('and'))   # 35
print(mystr.rfind('and', 15, 30))   # 23
print(mystr.rfind('ands'))  # -1

1.3 index():检测某个子串是否包含在这个字符串中,如果在,返回这个字串开始的位置下标,否则报异常

  1. 语法
    str.index('子串',开始位置下标,结束位置下标)
  2. demo
mystr = "Hello world and itcast and itheima and Python"

print(mystr.index('and')) # 12
print(mystr.index('and',15,30)) # 23
print(mystr.index('ands')) # 报异常

1.4 rindex(): 从右侧查找

demo

mystr = "Hello world and itcast and itheima and Python"

print(mystr.rindex('and'))   # 35
print(mystr.rindex('and', 15, 30))   # 23
print(mystr.rindex('ands'))  # rindex查找子串不存在,报错

1.5 count():检测子串出现的次数

demo

mystr = "Hello world and itcast and itheima and Python"

print(mystr.count('and', 15, 30))   # 1
print(mystr.count('and'))   # 3
print(mystr.count('ands'))  # 0

二、修改

2.1 replace():替换

  1. 语法
    str.replace('oldstr','newstr',替换次数)
  2. demo
myStr = "Hello world and itcast and itheima and Python"

# replace函数有返回值,返回值是修改后的字符串
new_str = myStr.replace("and", "he", 2)
print(myStr)    # 源字符串没有发生改变
print(new_str)

2.2 split():按照指定字符分割字符串,返回一个列表,丢失分割字符

demo

myStr = "Hello world and itcast and itheima and Python"

list1 = myStr.split('and')
print(list1)

list1 = myStr.split('and', 2)
print(list1)

2.3 join():合并列表里面的字符串数据为一个大字符串

demo

myList = ['aa', 'bb', 'cc']

# aa...bb...cc
new_str = '...'.join(myList)
print(new_str)

三、字符串大小写转换

3.1 capitalize():将字符串第一个字符转换成大写

myStr = "hello world and itcast and itheima and Python"

new_str = myStr.capitalize()
print(new_str)

结果

Hello world and itcast and itheima and python

3.2 title():将字符串每个单词首字母转换成大写

myStr = "hello world and itcast and itheima and Python"

new_str = myStr.title()
print(new_str)

结果

Hello World And Itcast And Itheima And Python

3.3 lower():将字符串中大写转小写

myStr = "hello world and itcast and itheima and Python"

new_str = myStr.lower()
print(new_str)

结果

hello world and itcast and itheima and python

3.4 upper():将字符串小写转换成大写

myStr = "hello world and itcast and itheima and Python"

new_str = myStr.upper()
print(new_str)

结果

HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON

四、删除空白字符

4.1 lstrip():删除字符串左侧空白字符

4.2 rstrip():删除字符串右侧空白字符

4.3 strip():删除字符串两侧空白字符

五、字符串对齐

5.1 ljust():返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长队的新字符串

5.2 rjust():右对齐

5.3 center():中间对齐

六、判断开头或结尾

6.1 startswith():检查字符串是否以指定子串开头,是则返回True,否返回False。如果设置开始和结束位置下标,则在指定范围内检查

mystr = "hello world and itcast and itheima and Python"

# 1. startswith()
print(mystr.startswith("hello"))    # True
print(mystr.startswith("hel"))  # True
print(mystr.startswith('hels')) # False

6.2 endswith():。。。结尾

mystr = "hello world and itcast and itheima and Python"

# 2.endswith()
print(mystr.endswith("Python")) # True
print(mystr.endswith("Pythons"))    # False

七、判断字符

7.1 isalpha():如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False

mystr1 = "hello"
mystr2 = "hello12345"

print(mystr1.isalpha()) # True

print(mystr2.isalpha()) # False

7.2 isdigit():判断是否纯数字

mystr2 = "hello12345"
mystr3 = "2341"

print(mystr3.isdigit())  # True
print(mystr2.isdigit())     # False

7.3 isalnum():数字或字母或组合

mystr1 = "hello"
mystr2 = "hello12345"
mystr3 = "2341"

print(mystr2.isalnum())  # True
print(mystr3.isalnum())  # True
print(mystr1.isalnum())  # True
mystr = "abdc.123"
print(mystr.isalnum())      # False

7.4 isspace():都是空白

mystr = " "
mystr1 = "hello world"
print(mystr.isspace())  # True
print(mystr1.isspace())     # False
原文地址:https://www.cnblogs.com/qian-yuan/p/14601988.html