# 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())
Python字符串
最新推荐文章于 2023-04-08 09:23:11 发布