python字符串/查找/修改/判断等函数

本文详细介绍了Python中字符串的输入、切片、查找、修改和判断等操作。包括单引号、双引号、三引号的使用,字符串切片的语法及注意事项,find()、index()、count()、rfind()、rindex()等查找函数,replace()、split()、join()等修改和分割方法,以及capitalize()、title()、upper()、lower()等字符串格式化函数。此外,还涉及了startswith()、endswith()等字符串判断功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.输入

字符串输入有四种方式:

        1)单引号输入

        2)双引号输入

        3)三单引号输入

        4)三双引号输入

单引号和双引号的输入效果是一样的,三单引号输入和三双引号输入在输入时换行,输出时也会换行

# 单引号字符串 换行会自动增加 \
a = 'my name ' \
    'is tom'
print(a)
print(type(a))

# 双引号字符串  换行会自动增加 \
b = "my name " \
    "is tom"
print(b)

# 三引号字符串 支持换行,换行后输出也会换行
c = '''my name is tom,
    from guandong'''
print(c)
#  三引号字符串  支持换行,换行后输出也会换行
d = """my name is tom,
    from guandong"""
print(d)

2.字符串切片

        语法:字符串序列[开始位置坐标,结束位置坐标,长度]

        注意:1)输出的数据不包含结束位置的数据

                   2)如果坐标不标名,默认开始坐标为 0,默认结束坐标为最后一个

                   3)坐标和长度正负数都可以,长度是选取间隔,如果是负数,表示倒叙

str1 = 'abcdefg'
print(str1[0:3:1])  # 输出 abc
print(str1[0:-1:2])  #输出  ace
print(str1[-4:-1:1])   #输出 def
print(str1[1:4:-1])   # 输出  无
print(str1[6:2:-1])   # 输出 gfed

3.字符串查找

  •         find() 函用法
字符串序列.find(子串,开始位置下标,结束位置下标)

        注意:当查找的子串不存在时返回 -1

str1 = 'hello world and itheima and python'
# 用法 find(字串,开始位置下标,结束位置下标)
# find() 查找
print(str1.find('and'))
print(str1.find('and', 15, 30))
print(str1.find('ands'))  # 不存在返回 -1
  •            index() 函数用法
字符串序列.index(子串,开始位置下标,结束位置下标)

        注意:当查找的子串不存在时会报错

str1 = 'hello world and itheima and python'
# 用法 index(字串,开始位置下标,结束位置下标)
# index() 查找
print(str1.index('and'))
print(str1.index('and', 15, 30))
print(str1.index('ands'))  # 不存在报错
  •            count() 函数用法

字符串序列.count(子串,开始位置,结束位置)

会返回子串的个数

str1 = 'hello world and itheima and python'
print(str1.count('and'))  #  返回 2
print(str1.count('and',0,20))  #  返回  1
  •           rfind()和rindex() 两个函数的用法
字符串序列.rfind(子串,开始位置,结束位置)
字符串序列.rindex(子串,开始位置,结束位置)

                都是从右边开始查找

str1 = 'hello world and itheima and python'
# rfing() 和find用法一样  但是从右侧开始查找
print(str1.rfind('and'))
# rindex() 和index用法一样  但是从右侧开始查找
print(str1.rindex('and'))

4.字符串修改

  •      replace() 函数用法:替换
字符串序列.replace(子串,要替换的字符串,参数)

                该函数不会改变原来的字符串,会返回一个新的字符串,参数表示要替换的次数

str1 = 'hello world and itheima and python and c++'
# replace() 字符串替换
new_str = str1.replace('and', 'he')  # 该函数不会改变原来的字符串,会返回一个新的字符串
new_str = str1.replace('and', 'he', 1)  #参数表示替换的次数
print(str1, "\n", new_str)
  •         split() 函数的用法:分割字符串
字符串序列.split(字符串,参数)

                该函数会返回一个列表,但是会丢失分割的字符串,参数表示要分割的次数

# split() 分割函数,会返回一个列表,丢失分割字符
list1 = str1.split('and')
list2 = str1.split('and', 2)  # 参数表示分割次数
print(list1)
print(list2)


输出
['hello world ', ' itheima ', ' python ', ' c++']
['hello world ', ' itheima ', ' python and c++']
  •         join() 函数用法:合并字符串
字符串或字符.join(列表)
# join()  合并字符串
# 字符串或字符.join(列表)
mylist = ['a', 'b', 'c']
new_str = ' '.join(mylist)
print(new_str)  


# 输出 
a b c
  •         capitalize() 函数用法:把字符串首字母转换成大写
字符串序列.capitalize()
  •         title() 函数用法:把所有单词首字母转换成大写
字符串序列.title()
  •         upper() :把字符串所有字母转换成大写        
  •         lower() :小写转大写
  •         lstrip():删除左侧空白字符
  •         rstrip:删除右侧空白字符
  •         strip:删除左右空白字符
  •         ljust:左对齐
  •         rjust:右对齐
  •        center:中间对齐

# upper() 把所有字母转换成大写 lower() 小写转大写
# new_str1 = str1.upper()
# print(new_str1)

# str2 = '        hello world and itheima and python and c++     '
# # lstrip() 删除左侧空白字符
# new_str1 = str2.lstrip()
# print(new_str1)
# # rstrip() 删除右侧空白字符
# new_str2 = str2.rstrip()
# print(new_str2)
# # strip() 删除左右空白字符
# new_str3 = str2.strip()
# print(new_str3)

str3 = 'hello world and itheima'
# ljust() 左对齐
new_str1 = str3.ljust(50, '.')
print(new_str1)
# rjust() 右对齐
new_str2 = str3.rjust(50, '.')
print(new_str2)
# center() 中间对齐
new_str3 = str3.center(50, '.')
print(new_str3)

5.字符串判断

  •        startswith() 函数的用法:判断是否以某个字符串开始,如果是返回 True,否则返回 False
字符串序列.startswith(子串,开始位置下标,结束位置下标)
str1 = 'hello word and itheima'
# startswith() 判断是否以某个字符串开始 如果是返回 True  否则返回 False
# 语法 字符串序列.startswith(子串,开始位置下标,结束位置下标)
print(str1.startswith('hell'))
print(str1.startswith('lo', 3, 10))
print(str1.startswith('hels'))
  •         endswith() 函数的用法:判断是否以某个字符串结尾,如果是返回 True,否则返回 False
字符串序列.endswith(子串)
str1 = 'hello word and itheima'
# # endswith() 判断是否以某个字符串结尾
print(str1.endswith('ima'))
print(str1.endswith('im'))
  •         isalpha():如果字符串全是字母,返回True,否则返回False
  •         isdigit():如果字符串全是数字则返回True,否则返回False
  •         isalnum():数字或字母或组合都返回True
  •         isspace():如果是空白返回True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱笑的蛐蛐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值