(六)Python基础,字符串(下)

2021/4/16 12:29:12

本文主要是介绍(六)Python基础,字符串(下),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#字符串:
##首字母大写,其他位置字母小写
# test = "QSGlhqQSG"
# v=test.capitalize()
# print(v)

#把所有变小写,casefold()更加牛逼,很多未知的相对应的变小写,lower()功能相对于较小,只适应于英文字母大小写。
# v1=test.casefold()
# print(v1)
# v2=test.lower()
# print(v2)

# 占用20字节位置,把字符串放在中间,后面的参数用来填充剩余的空白。--空白位置填充,只能一个字符串,可有可无
# v3=test.center(20,"中")
# print(v3)
# test = 'qsglhq'
# v1 = test.ljust(20,'*')  #字符串靠左
# v2 = test.rjust(20,'*')  #字符串靠右
# v3 = test.zfill(20)      #字符串靠右,只能用0填充
# print(v1,v2,v3)

#去字符串中寻找,寻找子序列的出现次数。
# v = test.count("lh",3,5)
# print(v)

# v = test.encode()
#
# v = test.decode()

#以什么开始,以什么结束
# v = test.endswith('SG')
# print(v)
# v1 = test.startswith('QS')
# print(v1)

#start和end参数,start位为子字符串第一个字符,end位为子字符串最后一个字符再往后移动一位。
# v2 = test.startswith('lhq',3,6)
# print(v2)
# v3 = test.endswith('qQ',3,7)
# print(v3)


#\t为空格标签,从左往右数,每6个一组,不够的用空格补,刚好6位用6空格,参数是整形定义填充多少位,默认8位。
# test1 = "1\t234567\t89"
test2 = "name\temail\tpass\nlhq\t18877490764.163.com\tlhq960305\nlhq\t18877490764.163.com\tlhq960305\nlhq\t18877490764.163.com\tlhq960305\n" \
#        "lhq\t18877490764.163.com\tlhq960305\n"
# s1 = test1.expandtabs(6)
s2 = test2.expandtabs(25)
# print(s1,len(s1)) ####1     234567      89 20####
print(s2)

#从前往后找,找到第一个匹配子字符串之后,获取其第一个字符的位置,
#start与end参数:7<=e<9  start定义的是开区间,end定义的是闭区间。
# test = "alexalexa"
# e = test.find('xa',7,9)
# print(e) ####没找到是 -1
#相似方法:index  去区别在于index找不到程序报错。




#格式化,将一个字符串中的占位符替换成指定的值
# test = "i am {name}, age {a}"
# print(test)
# v = test.format(name = 'lhq',a = 25)
# print(v)
# # 类型二:使用顺序替换
# test = "i am {0}, age {1}"
# v = test.format('lhq',25)
# print(v)

#格式化,传入值以键值对的形式{'name':'lhq','a':25}
# test = "i am {name}, age {a}"
# v1 = test.format(name = 'lhq',a = 25)
# v2 = test.format_map({'name':'lhq','a':25})
# print(v1)
# print(v2)


#判断字符串中是否都是数字和字符串,结果以布尔形式呈现。
# test1 = "123asd!@#"
# test2 = "123asd"
# v1 = test1.isalnum()
# v2 = test2.isalnum()
# print(v1)
# print(v2)

#是否都是字母、汉字
# test = 'ascd'
# test = 'asA梁鸿倩cd'
# v = test.isalpha()
# print(v)

#判断当前是否是数字,isdigit 特别的数字也行 isnumeric 最牛
# test1 = '1234'  #True  True#
# test2 = '②'    #False True#
#test3 = '二'     #False False True
# v1 = test3.isdecimal()  #十进制小数
# v2 = test3.isdigit()
# v3 = test3.isnumeric()
# print(v1,v2,v3)

#如果字符串是有效标识符,则 isidentifier() 方法返回 True,否则返回 False。
#如果字符串仅包含字母数字字母(a-z)和(0-9)或下划线(_),则该字符串被视为有效标识符。有效的标识符不能以数字开头或包含任何空格。
# txt = "Demo"
# x = txt.isidentifier()
# print(x)
#更多示范
# a = "MyFolder"   #True
# b = "Demo002"    #True
# c = "2bring"     #False  不能以数字开头
# d = "my demo"    #False   不能有空格
# print(a.isidentifier())
# print(b.isidentifier())
# print(c.isidentifier())
# print(d.isidentifier())

#大小写转换
# test = 'QSGlhq'
# v = test.swapcase()
# print(v)

# import keyword
# print(keyword.iskeyword('def'))
# test = 'def'
# v = test.isidentifier()
# print(v)

#是否存在不可显示的字符
#\t 制表符
#\n 换行符
# test = 'qeqww\nesa'
# v = test.isprintable()
# print(v)

#判断是否全部都是空格
# test = ' '
# v = test.isspace()
# print(v)

#判断是否是表标题,
# #标题里面每个单词首字母都是大写、
# test = 'Return True if the string is a title-cased string, False otherwise'
# # test = 'Return True'
# # v = test.istitle()
# # print(v)
#
# #把字符串转换成标题
# v = test.title()
# print(v)

#将字符串中的每一个元素安装指定分隔符进行拼接
# test = '你是风儿我是沙'
# t = ' '
# print(t.join(test))    #你 是 风 儿 我 是 沙
# print(' '.join(test))  #你 是 风 儿 我 是 沙
# print('*'.join(test))  #你*是*风*儿*我*是*沙


#判断是否是大小写,和大小写转换
# test = 'Qsglhq'
# # v1 = test.islower()
# # v2 = test.lower()
# # print(v1,v2)
# v1 = test.isupper()
# v2 = test.upper()
# print(v1,v2)

#去除左右空白,不能去除中间空白,\n去除换行,去除\t空白符,还可以去除指定字符
# test1 = '  qsglhq'
# test2 = '\nqsglhq\t'
# v1 = test1.lstrip()
# v2 = test1.rstrip()
# v3 = test1.strip()
# v4 = test2.strip()
# print(test2,v4)
# v5 = test1.rstrip('lhq')
# print(v5)


#maketrans 与 translate 协作,以前者规则替换字符串
# v = "lsahfoifsfsfseiufhwihweitfbgvnidgukljrw"
# m = str.maketrans("aeiou",'ABCDE')
# new_v = v.translate(m)
# print(new_v)



#partition从左边分割,三部分,中间是分割子集的本身,
# test = "maketrkeakns"
# # v = test.partition('ke')
# # print(v)
# # #rpartition 从右边分,其他和partition一样。
# # v1 = test.rpartition('ke')
# # print(v1)
#
# #分割字符串,不带分割子集本身,不写分割次数默认全部分,rsplit从右边开始分。
# v = test.split('k')
# print(v)   #['ma', 'etr', 'ea', 'ns'] 全分了
# v1 = test.split('k',2)
# print(v1)  #['ma', 'etr', 'eakns'] 只分了两次
# v2 = test.rsplit('k',1)
# print(v2)  #['maketrkea', 'ns']

# test = 'fsdhfr\ndwekufhwf\ndweiuhftopgj\ndwajdgh'
# v = test.splitlines()
# print(v)  #['fsdhfr', 'dwekufhwf', 'dweiuhftopgj', 'dwajdgh']
# # True   #带换行符
# v1 = test.splitlines(True) #['fsdhfr\n', 'dwekufhwf\n', 'dweiuhftopgj\n', 'dwajdgh']
# print(v1)
# # False  不带换行符
# v2 = test.splitlines(False)
# print(v2) #['fsdhfr', 'dwekufhwf', 'dweiuhftopgj', 'dwajdgh']

#以什么开始,以什么结尾
# test = 'kajhdfak.1.1.1'
# v = test.startswith('ka')
# print(v)
# v1 = test.endswith('.1')
# print(v1)

#替换字符串中的字符,后面带int参数是替换次数
# test = '123a456a789a0'
# v = test.replace('a','***')
# v1 = test.replace('a','##',2)
# print(v)
# print(v1)


##################七个基本魔法#############################
# join
# split
# find
# strip
# upper
# lower
#replace

####################四个灰魔法############################
#索引、下标、获取字符串中的某一个字符
# test = '覃世广和梁鸿倩'
# v = test[0]
# print(v)
# v1 = test[0:2]  #0<= v1 <2    切片
# print(v1)       #qs
# v2 = test[0:-1] #不要最后一个
# print(v2)      #qsglh
#
# #获取字符串长度
# v = len(test)
# print(v)
#
# for ql in test:
#     print(ql)


#四个灰魔法:索引、切片、for循环、len()


####################一个深灰魔法############################
##!字符串创建就不可以修改,一旦想修改或者拼接都是重新创建字符串


# for 循环中 break和continue也同样受用
# test = '覃世广12345657'
# for qin in test:
#     print(qin)
#     break
# for qin in test:
#     continue
#     print(qin)

#range获取连续的数字,后面加步长可以使其不连续。
# v = range(100)
# print(v)
# for p in v:
#     print(p)
# v = range(0,100,5)
# for p in v:
#     print(p)


# test = input(">>>")
# for i in test:
#     v=test.find(i)
#     print(v)

# s = input('>>>')
# num = len(s)
# v = range(0,num)
# for i in v:
#     print(i,s[i])




这篇关于(六)Python基础,字符串(下)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程