Python3 字符串常见操作

  • 字符串的查询操作
  • 字符串的格式操作
  • 字符串的分割操作
  • 字符串的判断操作
  • 字符串的拼接操作


字符串的查询操作

my_str = "Hello World"
# find(sub, start, end)
# 检查 sub 是否包含在 my_str 中,如果是返回开始的索引值,否则返回-1.
# start 与 end 参数查询 起始下标 终点下标
print(my_str.find("e"))     # 1
print(my_str.find("a"))     # -1
print(my_str.find("e", 0, len(my_str)))     # 1

# rfind(sub, start, end)
# 不同于 find()方法的是从右边开始查找
# 返回的依然是正向下标
print(my_str.rfind("o"))    # 7

# index(sub, start, end)
# 同 find() 方法使用一样,区别在于如果 sub 不存在会报异常

# rindex(sub, start, end)
# 不同于 index()方法的是从右边开始查找

# count(sub, start, end)
# 查询 sub 在 my_str 中出现的次数
print(my_str.count("e"))    # 1

字符串的格式操作

my_str = "hello world"

# capitalize()
# 将字符串一个字符大写
print(my_str.capitalize())  # Hello world

# title()
# 把字符串每个单词首字母大写
print(my_str.title())   # Hello World

# lower()
# 将字符串中所有大写字符转换为小写
my_str1 = "Hello World"
print(my_str1.lower())  # hello world

# upper()
# 将字符串中所有小写字母转换为大写
print(my_str.upper())   # HELLO WORLD

# ljust(width, fillchar)
# 字符串左对齐 fillchar 默认为空格
# 表示用空格填充至长度 width 的新字符串
print(my_str.ljust(15, "-"))    # hello world----

# rjust(width, fillchar)
# 字符串右对齐 fillchar 默认为空格
# 表示用空格填充至长度 width 的新字符串
print(my_str.rjust(15, "-"))    # ----hello world

# center(width, fillchar)
# 字符串居中 参数同上
print(my_str.center(15, '-'))   # --hello world--

my_strl = "   hello"
# lstrip(char)
# 删除字符串左边空格
# 参数 char 默认为空格 可设置删除的字符
print(my_strl.lstrip())     # hello

my_strr = "hello    "
# rstrip(char)
# 删除字符串右边空格
print(my_strr.rstrip())     # hello

my_strc = "  hello  "
# strip(char)
# 删除字符串两边空格
print(my_strc.strip())     # hello

字符串的分割操作

my_str = "hello world"

# partition(sep)
# 将字符串以 sep 分割成三部分 sep前 sep sep后
print(my_str.partition("wo"))   # ('hello ', 'wo', 'rld')

# rpartition(sep)
# 同 partition() 函数,不过是从右边开始
print(my_str.rpartition("wo"))   # ('hello ', 'wo', 'rld')

# splitlines()
# 按行分割
my_strn = "hello\nworld"
print(my_str.splitlines())  # ['hello world']

# split(str,maxsplit)
# 以 str 为分隔符切片 my_strs,str 默认为空格
# 如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串
my_strs = "he ll o wo rl d"
print(my_strs.split())  # ['he', 'll', 'o', 'wo', 'rl', 'd']

字符串的判断操作

my_str = "hello world"

my_num = "123456"

# isalpha()
# 如果 my_str 所有字符都是字母 则返回 True,否则返回 False
print(my_str.isalpha())     # False

# isdigit()
# 如果 my_num 只包含数字则返回 True 否则返回 False.
print(my_num.isdigit())     # True

# isalnum()
# 如果 my_num 所有字符都是字母或数字则返回 True,否则返回 False
print(my_str.isalnum())     # False

# isspace()
# 如果 mystr 中只包含空格,则返回 True,否则返回 False.
print(my_str.isspace())     # False

# startswith(obj)
# 检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False
print(my_str.startswith("he"))      # True

# endswith(obj)
# 检查字符串是否以 obj 结束,如果是返回True,否则返回 False.
print(my_str.endswith("ld"))    # True

字符串的拼接操作

str1 = "hello"
str2 = "world"

# + 加号拼接
print(str1 + str2)      # helloworld

# , 逗号拼接
print(str1, str2)       # hello world

# join(iterable) 拼接
print("".join(['a', 'b', 'c']))     # abc

# 格式化拼接
print("%s%s" % (str1, str2))    # helloworld
print("{}{}".format(str1, str2))    # helloworld
print(f"{str1}{str2}")  # helloworld