Python字符串

# 1.格式
# name = input("请输入你的名字:")
# print("你的名字是:%s" % name)
# print(f"你的名字是:{name}")
# str1 = 'abcdefg'
# for i in range(len(str1)):
#     print("第%d个字符是:%s" % (i, str1[i]))

# 2.切片
# print("第1-3个字符是:%s" % str1[1:3:1])  # [1:3:1]:从1到3(包含2不包含3),步长为1
# print("第1-3个字符是:%s" % str1[1:3])
# print("第1-3个字符是:%s" % str1[3:])
# print(str1[-2:-5])

# 3.查找
# find/index(find查找不到返回-1,index查找则报错)
# str1 = "shi dizai 666"
# str2 = "dizai"
# str3 = " dizai"
# print(str1.find(str2))  # 查找子串的位置
# print(str1.find(str2, 0, 9))  # 指定范围内查找子字符串str2
# print(str1.find(str3, 0, 11))  # 指定范围内查找子字符串str3
# print(str1.index(str3, 0, 11))  # 指定范围内查找子字符串str3
# print(str1.count('i', 0, 11))  # 指定范围内查找子字符串str3
# print(str1.count('ab', 0, 11))  # 指定范围内查找子字符串str3
#
# # rfind()和rindex()功能同上,区别就是从右开始查找
# print(str1.rfind('di', 0, 11))  # 指定范围内查找子字符串str3

# 4.字符串修改replace()
# mystr = 'hello word and i like and python and shidizai'
# newstr1 = mystr.replace('and', 'he')
# newstr2 = mystr.replace('and', 'he', 2)  # 2表示替换的次数
# print(newstr1)
# print(newstr2)

# 5.字符串分割split()  返回一个列表(并丢失分割字符)
# mystr = 'hello word and i like and python and shidizai'
# list1 = mystr.split('and')
# list2 = mystr.split('and', 2)  # 数字代表分割的次数
# print(list1)
# print(list2)


# 6.join()函数
# mylist = ['aa', 'bb', 'cc']
# # aa...bb...cc
# newstr = '...'.join(mylist)
# print(newstr)

# 7. capitalize()将字符串第一个字符转换成大小写(会把其他单词的首字母改成小写)
# mystr = 'hello word and i like and Python and shidizai'
# print(mystr.capitalize())


# 8.title()将字符串每个单词首字母转换成大写
# mystr = 'hello word and i like and python and shidizai'
# print(mystr.title())

# 9.upper() 小写转大写
# mystr = 'HELLO word and i like and python and shidizai'
# print(mystr.upper())

# 9.lower() 大写转小写
# mystr = 'HELLO word and i like and python and shidizai'
# print(mystr.lower())

# 10.lstrip()删除字符串左侧空白字符
# rstrip()删除字符串右侧空白字符
# strip()删除字符串左右侧空白字符
# mystr = '    HELLO word and i like and python and shidizai      '
# print(mystr)
# print(mystr.lstrip())
# print(mystr.rstrip())
# print(mystr.strip())

# 11.ljust()返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的新字符串
# 字符串序列.ljust(长度,填充字符)
# mystr = 'hello'
# print(mystr.ljust(10,'.'))
# print(mystr.rjust(10,'.'))
# print(mystr.center(11,'.'))


# 12.startswith()检查字符串是否以指定子串开头,是则返回True,否则返回False.如果设置开始和结束位置下标,则在指定范围内检查
# 字符串序列.startswith(子串,开始位置下标,结束位置下标)
# mystr = 'HELLO word and i like and python and shidizai'
# print(mystr.startswith('HELLO wo'))
# print(mystr.startswith('HELLO wo',2,10))
# print(mystr.startswith('LO word',3,10))

# 12.endswith()检查字符串是否以指定子串结尾,是则返回True,否则返回False.如果设置开始和结束位置下标,则在指定范围内检查
# 字符串序列.startswith(子串,开始位置下标,结束位置下标)
# mystr = 'HELLO word and i like and python and shidizai'
# print(mystr.endswith('dizai'))
# print(mystr.endswith('dizai',1,45))

# 13.isalpha()判断一个字符串至少有一个字符并且所有字符都为字母
# mystr = 'Hello'
# print(mystr.isalpha())

# 14.isdigit()判断一个字符串至少有一个字符并且所有字符都为数字
# mystr = '12345'
# print(mystr.isdigit())

# 15.isalnum()判断一个字符串是数字或字符或字母和数字的组合
# mystr = '12345abc'
# print(mystr.isalnum())

# 16.isspace()判断一个字符串是空格
mystr = '           '
print(mystr.isspace())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

万里守约

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

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

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

打赏作者

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

抵扣说明:

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

余额充值