014:字符串:各种其奇葩的内置方法

笔记

1.字符串也不可修改!!改变后标签也改变了
2.字符串的方法及注释

符号 说明
capitalize() 把字符串的第一个字符改为大写
casefold() 把整个字符串的所有字符改为小写
center(width) 将字符串居中,并使用空格填充至长度width的新字符串
count(sub[,start[,end]]) 返回sub在字符串里边出现的次数,start和end参数表示范围,可选。
encode(encoding='utf-8',errors='strict') 以encoding指定的编码格式对字符串进行编码。
endswith(sub[,start[,end]]) 检查字符串是否以sub子字符串结束,如果是返回True,否则返回False。start和end参数表示范围,可选。
expandtabs([tabsize=8]) 把字符串中的tab符号( )转换为空格,如不指定参数,默认的空格数是tabsize=8。
find(sub[,start[,end]]) 检测sub是否包含在字符串中,如果有则返回索引值,否则返回-1,start和end参数表示范围,可选。
index(sub[,start[,end]]) 跟find方法一样,不过如果sub不在string中会产生一个异常。

isalnum() 如果字符串至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False。
isalpha() 如果字符串至少有一个字符并且所有字符都是字母则返回True,否则返回False。
isdecimal() 如果字符串只包含十进制数字则返回True,否则返回False。
isdigit() 如果字符串只包含数字则返回True,否则返回False。
islower() 如果字符串中至少包含一个区分大小写的字符,并且这些字符都是小写,则返回True,否则返回False。
isnumeric() 如果字符串中只包含数字字符,则返回True,否则返回False。
isspace() 如果字符串中只包含空格,则返回True,否则返回False。
istitle() 如果字符串是标题化(所有的单词都是以大写开始,其余字母均小写),则返回True,否则返回False。
isupper() 如果字符串中至少包含一个区分大小写的字符,并且这些字符都是大写,则返回True,否则返回False。

|join(sub) |以字符串作为分隔符,插入到sub中所有的字符之间。>>> str5 = 'Fishc' >>> str5.join('12345') '1Fishc2Fishc3Fishc4Fishc5'
|ljust(width) |返回一个左对齐的字符串,并使用空格填充至长度为width的新字符串。
|lower() |转换字符串中所有大写字符为小写。
|lstrip() |去掉字符串左边的所有空格
|partition(sub) |找到子字符串sub,把字符串分成一个3元组(pre_sub,sub,fol_sub),如果字符串中不包含sub则返回(‘原字符串’, ’’, ’’)
|replace(old,new[,count])|把字符串中的old子字符串替换成new子字符串,如果count指定,则替换不超过count次。
>>> str7 = 'i love fishdm and seven'
>>> str7.replace('e','E',2)
'i lovE fishdm and sEven'
|rfind(sub[,start[,end]]) |类似于find()方法,不过是从右边开始查找。
|rindex(sub[,start[,end]]) |类似于index()方法,不过是从右边开始。
|rjust(width) |返回一个右对齐的字符串,并使用空格填充至长度为width的新字符串。
|rpartition(sub) |类似于partition()方法,不过是从右边开始查找。
|rstrip() |删除字符串末尾的空格。
|split(sep=None, maxsplit=-1)|不带参数默认是以空格为分隔符切片字符串,如果maxsplit参数有设置,则仅分隔maxsplit个子字符串,返回切片后的子字符串拼接的列表。
>>> str7.split () ['i', 'love', 'fishdm', 'and', 'seven']
|splitlines(([keepends]))|按照‘ ’分隔,返回一个包含各行作为元素的列表,如果keepends参数指定,则返回前keepends行。
|startswith(prefix[,start[,end]])|检查字符串是否以prefix开头,是则返回True,否则返回False。start和end参数可以指定范围检查,可选。
|strip([chars]) |删除字符串前边和后边所有的空格,chars参数可以定制删除的字符,可选。
|swapcase() |翻转字符串中的大小写。
|title() |返回标题化(所有的单词都是以大写开始,其余字母均小写)的字符串。
|translate(table)|根据table的规则(可以由str.maketrans(‘a’,‘b’)定制)转换字符串中的字符。
>>> str8 = 'aaasss sssaaa'
>>> str8.translate(str.maketrans('s','b'))
'aaabbb bbbaaa'
|upper() |转换字符串中的所有小写字符为大写。
|zfill(width) |返回长度为width的字符串,原字符串右对齐,前边用0填充。

测试题

1.如何定义一个跨越多行的字符串吗(请至少写出两种实现的方法)?

方法一:

str1 = '''待我长发及腰,将军归来可好?
此身君子意逍遥,怎料山河萧萧。
天光乍破遇,暮雪白头老。
寒剑默听奔雷,长枪独守空壕。
醉卧沙场君莫笑,一夜吹彻画角。
江南晚来客,红绳结发梢。'''

方法二:

str2 = '待卿长发及腰,我必凯旋回朝。
昔日纵马任逍遥,俱是少年英豪。
东都霞色好,西湖烟波渺。
执枪血战八方,誓守山河多娇。
应有得胜归来日,与卿共度良宵。
盼携手终老,愿与子同袍。'

方法三:

str3 = ('待卿长发及腰,我必凯旋回朝。'
'昔日纵马任逍遥,俱是少年英豪。'
'东都霞色好,西湖烟波渺。'
'执枪血战八方,誓守山河多娇。'
'应有得胜归来日,与卿共度良宵。'
'盼携手终老,愿与子同袍。')

2.(还原为有意义的字符串):str1='i2sl54ovvvb4e3bferi32s56h;$c43.sfc67o0cm99'

答:str1[::3]

3.请写一个密码安全性检查的脚本代码:check.py
此处输入图片的描述

temp = input('请输入要检查的密码:')
counta = 0
countb = 0
countc = 0
teshuzifu = "~!@#$%^&*()_=-/,.?<>;:[]{}|"
while True:
    for i in temp:
        if i in teshuzifu:
            counta += 1
        if i.isdigit():
            countb += 1
        if i.isalpha():
            countc += 1
    count = counta + countb + countc
    if 0 < count <= 8 and ((counta == 0 and countb == 0) or (counta == 0 and countc == 0)):
        print('低级')
        break
    else:     #elif
        if ((counta == 0 and countb * countc != 0) or (countb == 0 and countb * countc != 0) or (countc == 0 and countb * countc != 0)) and 8 < count <= 16:
            print('中级')
            break
        else:
            if counta != 0 and countb != 0 and countc != 0 and count >= 16:
                print('高级')
                break
            else:
                temp = input('请重新输入')

按照答案修改:

str1 = "~!@#$%^&*()_=-/,.?<>;:[]{}|"
has_str1 = 0
has_num = 0
has_alpha = 0
t = 'y'
while t == 'y':
        password = input("请输入需要检查的密码组合:")
        length = len(password)
        while(password.isspace() or length == 0):
                password = input('您输入的密码为空(或空格),请重新输入:')
        for i in password:
                if i in str1:
                        has_str1 = 1
                if i.isdigit():
                        has_num = 1
                if i.isalpha():
                        has_alpha = 1
        has =  has_str1 + has_num + has_alpha
        if length <= 8 or password.isalnum():
                level = "低"
        if length > 8 and has ==2:
                level = "中"
        if length >= 16 and has == 3 and password[0].isalnum():
                level = "高"
        print("您的密码安全等级评定为:%s"%level)
        if level == "高":
                print("请继续保持")
        else:
                print("""请按以下方式提升您的密码安全级别:
        1.密码必须由数字、字母及特殊字符三种组合
        2.密码只能由字母开头
        3.密码长度不能低于16位""")
        t = input("还要再测试么?(”y“继续,其他退出)")
原文地址:https://www.cnblogs.com/superrrrjia/p/8343782.html