python str基本用法

目录

1  没什么用的

1.1  调用实例属性后自动执行 __getattribute__()

1.2  实例化后自动调用 __new__() 

1.3  将其他变量转变为字符串 __repr__()

1.4  反向替代占位符(格式化) __rmod__()

1.5  字符串反向做乘法 __rmul__()

1.6  将变量转变为字符串 __str__() 

2  不常用的

2.1  字符相加 __add__()

2.2  判断字符是否在字符串中 __contains__ ()

2.3  判断字符串是否相等 __eq__()

2.4  大于等于 __ge__()

2.5  大于 __gt__()

2.6  返回哈希值 __hash__() 

2.7  小于等于 __le__()

2.8  小于 __lt__()

2.9  替代占位符(格式化) __mod__()

2.10  字符串做乘法 __mul__() 

2.11  不等于 __ne__() 

2.12  获取字符串相应位置的字符 __getitem__()

3  常用的

3.1  创建字符串 str() 

3.2  将其他变量转换为字符串 __format__() 

3.3 将变量转变为元组 __getnewargs__()

3.4  返回迭代器 __iter__()

3.5  返回变量字节大小 __sizeof__()

3.6  一段字符串中的第一个字符大写 capitalize()

3.7  所有字符变为小写 casefold()

3.8  居中摆放指定字符 center()

3.9  返回指定字符出现的次数 count()

3.10  返回指定编码 encode()

3.11  判断最后一个字符是否为指定字符 endswith()

3.12  更改制表符长度 expandtabs()

3.13  返回指定字符在字符串内的第一个位置(查找) find()

3.14  替代占位符(格式化) format()

3.15  映射替代 format_map()

3.16  返回指定字符在字符串内的第一个位置(查找) index()

3.17  判断字符串中是否全是文字或数字 isalnum()

3.18  判断字符串中是否全是文字 isalpha()

3.19  判断字符串中是否全是decimal数字 isdecimal()

3.20  判断字符中是否全为degits数字 isdigit()

3.21  判断字符串中的内容是否可以作为变量名使用 isdentifier()

3.22  判断所有字符是否均为小写 islower()

3.23  判断字符串中是否均为numeric数字 isnumeric()

3.24  判断字符串中是否有转义字符或回车 isprintable()

3.25  判断字符串中是否所有字符均为空格 isspace()

3.26  判断字符串中每一个单词首字母是否均为大写状态 istitle()

3.27  判断所有字符是否均为大写 isupper()

3.28  在可迭代变量b中加入指定字符a join()

3.29  补全指定字符长度 ljust()

3.30  将字符串中所有内容改为小写 lower()

3.31  从左侧起截取字符串中指定字符 lstrip()

3.32  按照指定分隔符将字符串分割为三个元素的元组 partition()

3.33  替换掉字符串中指定字符 replace()

3.34  从右侧在字符串中查找指定字符的位置 rfind()

3.35  从右侧在字符串中查找指定字符的位置 rindex()

3.36  指定字符串长度,在左侧进行补充 rjust()

3.37  从字符串右侧寻找分割点,之后分割为三个元素的元组 rpartition()

3.38  从右侧寻找将字符串中指定字符删除,之后返回列表 rsplit()

3.39  从右侧删除指定字符 rstrip() 

3.40  从左侧寻找将字符串中指定字符删除,之后返回列表 split()

3.41  分离每一行内容 splitlines()

3.42  判断起始字符串是否为指定字符 startswith()

3.43  删除两侧的指定字符 strip() 

3.44  小写变大写,大写变小写 swapcase() 

3.45  将每一个单词的首字母变为大写字母 title()

3.46  设置字符对应maketrans()与替换对应字符translate()

3.47  将所有内容改为大写 upper()

3.48  指定字符长度后之后用字符0填充 zfill() 


1  没什么用的

1.1  调用实例属性后自动执行 __getattribute__()

__getattribute__函数在调用实例属性时自动执行

a = 0
class man(object):
    gender = '男'
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def __getattribute__(self,something):
        global a
        a = a + 1
        print('调用次数',a)

A = man('小明',20)
print(man.gender)
print()
print(A.gender)
print()
print(A.gender)

a = 0
class man(object):
    gender = '男'
    def __init__(self,name,age):
        self.name = name
        self.age = age

A = man('小明',20)
print(man.gender)
print()
print(A.gender)
print()
print(A.gender)

1.2  实例化后自动调用 __new__() 

__new__函数与__init__作用相同,当实例化后自动调用且调用优先级在__init之前

class demoClass:
    def __init__(self):
        print('1')
    def __new__(self):
        print('2')

a = demoClass()

class demoClass2:
    def __init__(self):
        print('1')

b = demoClass2()

1.3  将其他变量转变为字符串 __repr__()

__repr__可以将其他类型的变量转换为字符串类型

a = 'hello'
b = a.__repr__()
print(b)
print(type(b))

a = 1
b = a.__repr__()
print(b)
print(type(b))

1.4  反向替代占位符(格式化) __rmod__()

这个在help文档中是错误的

在别的类型中它是将除数与被除数交换位置

这个也确实是反向的,它反向之后占位符要加在参数中

不过一般来讲要实现功能我们还是使用format

当__rmod__函数使用方式如下所示,与__mod__使用函数方式相反,但只能替换str,其他类型的不行

a = 'beautiful'
b = a.__rmod__('good morning %s')
print(b)
print(type(b))

如果替换其他类型它会返回一个非异常对象,返回不了我们所需要的结果,因为如果改变被操作变量它就不再是str内的方法了

c = 47
d = c.__mod__('hello %d')
print(d)

1.5  字符串反向做乘法 __rmul__()

正向乘法和反向乘法结果相同,比如 3*5 和 5*3

我们只能正向来用这个函数

a = 'hello'
b = a.__rmul__(3)
print(b)

如果反向来用就不是str内的方法,就不会返回我们想要的结果

a = 3
b = a.__rmul__('hello')
print(b)

1.6  将变量转变为字符串 __str__() 

a = 'hello'
b = a.__str__()
print(b)
print(type(b))

c = a.__repr__()
print(c)
print(type(c))

2  不常用的

2.1  字符相加 __add__()

__add__函数功能为在指定字符串后添加指定字符

a = '1'
b = '2'
print(a.__add__(b))
print(a.__add__('3'))

可以用加号替代 

a = '1'
b = '2'
c = a + b
print(c)
print(type(c))

2.2  判断字符是否在字符串中 __contains__()

__contains__函数为检验字符串是否在指定字符串中

a = 'hello'
print(a.__contains__('h'))
print(a.__contains__('e'))
print(a.__contains__('l'))
print(a.__contains__('0'))

可以用in代替

a = 'hello'
'h' in a

2.3  判断字符串是否相等 __eq__()

__eq__函数作用为确认两变量是否相等,相等返回True,eq含义为equal

a = '1'
b = '1'
c = '2'
print(a.__eq__(b))
print(b.__eq__(c))

可以用==代替

a = '1'
b = '1'
a == b

2.4  大于等于 __ge__()

__ge__大于等于,ge含义为greater equal

a = '0'
b = '1'
c = '1'
print(a.__ge__(b))
print(b.__ge__(a))
print(c.__ge__(b))

a = '0'
b = '1'
a >= b

2.5  大于 __gt__()

__gt__:大于,含义为greater than

a = '1'
b = '2'
c = '2'
print(a.__gt__(b))
print(b.__gt__(a))
print(c.__gt__(b))

a = '1'
b = '2'
b > a

2.6  返回哈希值 __hash__() 

若__hash__值相同,则__eq__函数结果为true

a = '1'
b = '2'
c = '2'
print(a.__hash__())
print(b.__hash__())
print(c.__hash__())
print(b.__eq__(c))

2.7  小于等于 __le__()

 __le__:小于等于,含义为less equal

a = '1'
b = '2'
c = '2'
print(a.__le__(b))
print(b.__le__(c))
print(c.__le__(a))

a = '2'
b = '2'
a <= b

2.8  小于 __lt__()

__lt__:小于,含义为less than

a = '1'
b = '2'
c = '2'
print(a.__lt__(b))
print(b.__lt__(c))
print(c.__lt__(a))

a = '1'
b = '2'
b < a

2.9  替代占位符(格式化) __mod__()

 在help文档中的解释是不对的

很明显,上面的方法是取余

我们在下面这个cell测试一下,会报错

a = '10'
b = '3'
c = a.__mod__(b)
print(c)
print(type(c))

它在str内真正的用法是这样的

#当字符串中有%占位时可使用__mod__函数
#替换字符串
a = 'good morning %s'
b = a.__mod__('beautiful')
print(b)
#替换十进制数字
c = 'hello %d'
d = c.__mod__(47)
print(d)
#替换八进制数字
c = 'hello %o'
d = c.__mod__(8)
print(d)
#替换十六进制数字(小写)
c = 'hello %x'
d = c.__mod__(15)
print(d)
#替换十六进制数字(大写)
c = 'hello %X'
d = c.__mod__(15)
print(d)
#替换6位小数,替换小数时可以为int也可以为float
c = 'hello %f'
d = c.__mod__(15.8)
print(d)
#替换3位小数
c = 'hello %.3f'
d = c.__mod__(15.8)
print(d)
#科学计数法保留小数点后面六位有效数字
c = 'hello %e'
d = c.__mod__(15.8)
print(d)
#科学计数法形式保留3位有效数字
c = 'hello %.3e'
d = c.__mod__(150.8)
print(d)

#以上例子为常用的代码,如遇到其他%占位可进行搜索

我们使用格式化一般使用format

a = 'good morning {}'
b = a.format('beautiful')
print(b)

a = 'good morning {} {}'
b = a.format('beautiful','hello')
print(b)

a = 'good morning {1} {0}'
b = a.format('beautiful','hello')
print(b)

2.10  字符串做乘法 __mul__() 

 __mul__功能为将指定字符串乘以相应个数

a = 'hello '
b = a.__mul__(2)
print(b)
print(type(b))

a = 'hello '
b = a * 5
print(b)
print(type(b))

2.11  不等于 __ne__() 

__ne__:不等于,含义为not equal

a = '1'
b = '2'
c = '2'
print(a.__ne__(b))
print(b.__ne__(c))

a = '1'
b = '2'
a != b

2.12  获取字符串相应位置的字符 __getitem__()

 __getitem__函数功能为获取相应位置的字符

a = 'hello'
print(a.__getitem__(0))
print(a.__getitem__(1))
print(a.__getitem__(2))
print(a.__getitem__(3))
print(a.__getitem__(4))

 一般我们用中括号

a = 'hello'
print(a[0])

3  常用的

3.1  创建字符串 str() 

a = 1
b = str(a)
print(b)
print(type(b))

3.2  将其他变量转换为字符串 __format__() 

 我们用所有ascii码试一试这个方法可以填什么参数

letter_list=[]
for letter in range(0,128):
    letter_list.append(chr(letter))
print(letter_list)

for var in letter_list:
    try:
        b = a.__format__(var)
        print(var,b,type(b))
    except:
        pass

首先我们看到的一点是所有的返回值类型都为字符串

之后我们可以明显看到1-9为指定字符位数,指定的几就是几位(多数出来一个空格的原因是print)

s是将变量转换为字符串,如果变量本来就是字符串就没有什么用了

< > ^这三个还不是很明显

我们将符号后面输入一个指定的位数

这个时候我们就可以很明显的看出区别了,分别是左对齐,右对齐,居中

#左对齐 <
a = 'hello'
b = a.__format__('<10')
print(b)

#右对齐 >
a = 'hello'
b = a.__format__('>10')
print(b)

#居中
a = 'hello'
b = a.__format__('^10')
print(b)

我们总结一下

  • 1-9 指定字符串位数
  • < 左对齐
  • > 右对齐
  • ^ 居中
  • s 转变为字符串

3.3 将变量转变为元组 __getnewargs__()

#__getnewargs__功能为将字符串转变为元组
a = 'hello'
b = a.__getnewargs__()
print(b)
type(b)

3.4  返回迭代器 __iter__()

#将字符串转换为迭代器
a = 'hello'
b = a.__iter__()
print(b)
print(type(b))
#使用next遍历迭代器的每一个元素
print(next(b))
print(next(b))
print(next(b))
print(next(b))
print(next(b))

#遍历之后报出StopIteration错误,在for循环的遍历中,自动处理StopIteration错误
print(next(b))

3.5  返回变量字节大小 __sizeof__()

#__sizeof__函数可显示变量字节大小,返回值为int形式
a = 'hello'
b = '你好'
print(a.__sizeof__())
print(b.__sizeof__())
c = a.__sizeof__()
print(type(c))

a = 'h'
print(a.__sizeof__())

a = 'he'
print(a.__sizeof__())

a = '你好'
print(a.__sizeof__())

a = '1'
print(a.__sizeof__())

3.6  一段字符串中的第一个字符大写 capitalize()

a = 'hello world'
b = a.capitalize()
print(b)

a = '我hello world'
b = a.capitalize()
print(b)

3.7  所有字符变为小写 casefold()

#casefold所有字母小写
a = 'HELLO WORLD'
b = a.casefold()
print(b)

3.8  居中摆放指定字符 center()

#center可是字符串在规定长度内居中
#必须提供规定长度的参数,可以选择提供填充其余字符的填充物
a = 'hello'
b = a.center(11)
print(b)

c = a.center(11,'*')
print(c)

3.9  返回指定字符出现的次数 count()

#count函数计算出提供字符在字符串内出现的次数
#必须提供想要计算的字符,可以选择提供规定的字符串区间
#使用时无法跳过start直接定义end参数
a = 'helll'
b = a.count('l')
print(b)

c = a.count('l',3)#从字符串的3号位数,默认为从3数到最后一位,包含3
print(c)

d = a.count('l',4,5)#从4数到5,包含4,5
print(d)

3.10  返回指定编码 encode()

#encode函数功能为编码,其中指定的编码方式有两种utf-8与GB2312
a = '你好'
b = a.encode('utf-8')
print(b)

c = a.encode('GB2312')
print(c)

d = a.encode('GBK')
print(d)

 第二个参数是出错误会提示的值,不影响编码结果,如果不设置的话默认为 strict

a = '你好'
b = a.encode('utf-8','hello')
print(b)

3.11  判断最后一个字符是否为指定字符 endswith()

#endswith函数确认指定字符串最后的字符是否为指定字符,如是则输出True
a = 'hello'
b = a.endswith('o')
print(b)
c = a.endswith('e')
print(c)

我们可以指定判断的起始点和终止点,在0,2之间最后一个字符是e,这个时候判定就为True

a = 'heallo'
c = a.endswith('a',0,3)
print(c)

3.12  更改制表符长度 expandtabs()

#expandtabs函数作用为如字符串内有制表符,更改制表符占字符位,默认为8位,可手动更改
a = '\t'
b = a.expandtabs()
print(b)

print('01234567890123456789')

d = a.expandtabs(16)
print(d)

 蓝色区域是我手动框选的,这样能清楚的看到效果

a = '\t'
print(a)
print('01234567890123456789')

3.13  返回指定字符在字符串内的第一个位置(查找) find()

#find函数功能位查找指定字符串指定字符的第一个位置
#find函数与下面的index函数作用相同
#两函数的区别为当使用find函数寻找字符串未存在的字符时,返回值为-1
#可设置寻找区间
a = 'hello'
b = a.find('l')
print(b)
print(type(b))

c = a.find('w')
print(c)

我们也可以指定它的起始位置与终止位置

下面这个例子我们找到的就是第二个 l 的位置

a = 'hello'
b = a.find('l',3,5)
print(b)

3.14  替代占位符(格式化) format()

#当字符串中有%占位时可使用format函数
#format函数为最常用的格式化函数,使用格式如下
#替换字符串
a = 'good morning {}'
b = a.format('beautiful')

print(b)
#替换数字
c = 'hello {}'
d = c.format(47)

print(d)
#替换小数
c = 'hello {}'
d = c.format(15.8)
print(d)

a = 'good morning {} {}'
b = a.format('beautiful','123')
print(b)

a = 'good morning {1} {0}'
b = a.format('beautiful','123')
print(b)

3.15  映射替代 format_map()

 下面我们替代一个键值对作为例子

#format_map功能为替换映射型变量,替换方式如下所示
a = 'hello {name}'
print(a.format_map({'name':'Suyu'}))

3.16  返回指定字符在字符串内的第一个位置(查找) index()

#index函数与上面的find函数作用相同
#两函数的区别为当使用index函数寻找字符串未存在的字符时会报错
a = 'hello'
b = a.index('l')
print(b)

c = a.index('w')
print(c)

 我们同样可以指定起始点与终止点

a = 'hello'
b = a.index('l',3,5)
print(b)

3.17  判断字符串中是否全是文字或数字 isalnum()

#isalnum函数用来检测指定字符串中是否存在空格与其他标点字符,如果存在,返回false
#isalnum含义为is alphabet number(其中可以包含中文字符)
a = 'hello'
b = a.isalnum()
print(b)

c = 'hello!'
d = c.isalnum()
print(d)

e = '我'
f = e.isalnum()
print(f)

g = '1'
h = g.isalnum()
print(h)

3.18  判断字符串中是否全是文字 isalpha()

#isalpha函数检测字符串内是否全为英文字符(包含中文字符)
#isalpha含义为is alphabet
a = 'hello'
b = a.isalpha()
print(b)

c = '12'
d = c.isalpha()
print(d)

a = 'hello!'
b = a.isalpha()
print(b)

a = '我'
b = a.isalpha()
print(b)

3.19  判断字符串中是否全是decimal数字 isdecimal()

#isdecimal函数识别范围如下所示
#True: Unicode数字,全角数字(双字节)
#False: 罗马数字,汉字数字,小数
#Error:byte数字
a = '123'  #Unicode数字
b = a.isdecimal()
print(b)

c = '123'#全角数字
d = c.isdecimal()
print(d)

e = 'Ⅳ'
f = e.isdecimal()
print(f)

g = '一二三'
h = g.isdecimal()
print(h)

i = '3.3'
j = i.isdecimal()
print(j)

k = b'123'
l = k.isdecimal()
print(l)

3.20  判断字符中是否全为degits数字 isdigit()

#isdigit()函数识别范围如下所示
#True: Unicode数字,byte数字(单字节),全角数字(双字节)
#False: 汉字数字,罗马数字,小数
#Error: 无
a = '123'  #Unicode数字
b = a.isdigit()
print(b)

c = '123'#全角数字
d = c.isdigit()
print(d)

e = 'Ⅳ'
f = e.isdigit()
print(f)

g = '一二三'
h = g.isdigit()
print(h)

i = '3.3'
j = i.isdigit()
print(j)

k = b'123'
l = k.isdigit()
print(l)

3.21  判断字符串中的内容是否可以作为变量名使用 isdentifier()

#isidentifier()函数为查看字符串能否作为变量名使用
a = 'hello'
b = a.isidentifier()
print(b)

c = '3a'
d = c.isidentifier()
print(d)

a = '我'
b = a.isidentifier()
print(b)

3.22  判断所有字符是否均为小写 islower()

#islower()函数True条件为所有字符均为小写状态,反之为false
a = 'HELLO'
b = a.islower()
print(b)

c = 'Hello'
d = c.islower()
print(d)

e = 'hello'
f = e.islower()
print(f)

3.23  判断字符串中是否均为numeric数字 isnumeric()

#isnumeric()检测范围如下所示
#True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
#False: 小数
#Error: byte数字(单字节)
a = '123'  #Unicode数字
b = a.isnumeric()
print(b)

c = '123'#全角数字
d = c.isnumeric()
print(d)

e = 'Ⅳ'
f = e.isnumeric()
print(f)

g = '一二三'
h = g.isnumeric()
print(h)

i = '3.3'
j = i.isnumeric()
print(j)

k = b'123'
l = k.isnumeric()
print(l)

3.24  判断字符串中是否有转义字符或回车 isprintable()

#isprintable()函数
#如果字符串可打印返回True,如果字符串不能打印(其中有回车或换行符)返回False
a = 'hello'
b = a.isprintable()
print(b)

c = 'hello \n'
d = c.isprintable()
print(d)

c = '''
hello
'''
d = c.isprintable()
print(d)

3.25  判断字符串中是否所有字符均为空格 isspace()

a = ' '
b = a.isspace()
print(b)

a = 'hello world'
b = a.isspace()
print(b)

3.26  判断字符串中每一个单词首字母是否均为大写状态 istitle()

#istitle()若每个单词的首字母都大写返回True,否则返回False,如果中间夹杂着别的字符不影响判定
a = 'hello world'
b = a.istitle()
print(b)

c = 'hello World'
d = c.istitle()
print(d)

e = 'Hello World'
f = e.istitle()
print(f)

e = 'Hello World我'
f = e.istitle()
print(f)

e = 'Hello World 我'
f = e.istitle()
print(f)

e = 'Hello World 1'
f = e.istitle()
print(f)

3.27  判断所有字符是否均为大写 isupper()

#isupper()如果字符串中所有字符为大写返回True,否则返回False
a = 'HELLO'
b = a.isupper()
print(b)

c = 'Hello'
d = c.isupper()
print(d)

3.28  在可迭代变量b中加入指定字符a join()

#join()在b的每一个字符中间空隙中插入a字符
a = ' 1 '
b = '23456'
c = a.join(b)
print(c)

3.29  补全指定字符长度 ljust()

#ljust()从左侧数指定字符长度,超出部分使用指定字符串补全
a = 'hello'
b = a.ljust(8,'-')
print(b)

#填充物为可选参数,若不给参数默认空格填充
a = 'hello'
b = a.ljust(7)
print(b)

3.30  将字符串中所有内容改为小写 lower()

#lower()将字符串中所有大写字符改为小写
a = 'HELLO'
b = a.lower()
print(b)

3.31  从左侧起截取字符串中指定字符 lstrip()

#lstrip()截掉字符串左侧的固定字符,默认为空格
#lstrip含义为left strip
a = '      h e l l o    1'
b = a.lstrip()
print(b)

c = 'hello'
d = c.lstrip('he')
print(d)

#当字符连续出现时截去连续的字符
e = 'hhheeeellol'
f = e.lstrip('hel')
print(f)

3.32  按照指定分隔符将字符串分割为三个元素的元组 partition()

#partition()将字符串分为头,分隔符,尾三段,参数输入分隔符
#使用该函数后的变量类型为元组
a = 'hello world'
b = a.partition(' ')
print(type(b))
print(b)

#如果输入的分隔符未在字符串中则将字符串置为元组0号元组,其余两个未空字符串
c = 'hello world'
d = c.partition('k')
print(type(d))
print(d)

a = '0.jpg'
b = a.partition('.')
print(b)

for c in b:
    if c == '.':
        break
    print(c)

3.33  替换掉字符串中指定字符 replace()

#replace()替换字符串中指定的字符串
#参数从前到后分别为:
#0.要替换的字符串   1.替换之后的结果  3.替换的数量
#其中3为可选参数如不输入则全部替换
a = 'hellohhh'
b = a.replace('h','k')
print(b)

c = 'hellohhh'
d = c.replace('h','k',3)
print(d)

3.34  从右侧在字符串中查找指定字符的位置 rfind()

#rfind()找到最高位的指定字符串,含义为reverse find
a = 'hello'
b = a.rfind('l')
print(b)


#可选参数与find相同,可设置起点与终点,包含左不包含右
#此处0,1,2等是从左侧数起
c = 'hello'
d = c.rfind('l',0,3)
print(d)

#如在区间内无指定字符,返回-1
c = 'hello'
d = c.rfind('l',0,2)
print(d)

3.35  从右侧在字符串中查找指定字符的位置 rindex()

#rindex()与rfind()功能相同,区别是使用rindex()时在字符串中没有指定字符串时会报错
#rindex含义为reverse index
a = 'hello'
b = a.rindex('l')
print(b)

c = 'hello'
d = c.rindex('k')

 

3.36  指定字符串长度,在左侧进行补充 rjust()

#rjust()指定字符串长度,从左侧进行填充
#rjust含义为right justified
a = 'hello'
b = a.rjust(8,'*')
print(b)
#填充物为可选参数,若给参数默认为空格填充
c = 'hello'
d = a.rjust(8)
print(d)

3.37  从字符串右侧寻找分割点,之后分割为三个元素的元组 rpartition()

#rpartition()与partition()功能相似,rpartition寻找分割点从右侧寻找
#rpartition含义为 resverse partition
a = 'hello world'
b = a.rpartition('l')
print(b)

3.38  从右侧寻找将字符串中指定字符删除,之后返回列表 rsplit()

#rsplit()将指定字符从字符串中删去,之后将字符串分离为列表,分离位置为指定字符串位置
#rsplit含义为reverse split
#下面这个例子为不加如可选参数count
a = 'hello'
b = a.rsplit('l')
print(b)

#下面这个列子将count置为1,则从最右侧删除一个指定字符
c = 'hello'
d = a.rsplit('l',1)
print(d)

3.39  从右侧删除指定字符 rstrip() 

#rstrip()从右侧删除字符,含义为right strip,默认为删除空格
a = '    hello    '
b = a.rstrip()
print(b)

c = 'ohello'
d = c.rstrip('o')
print(d)

3.40  从左侧寻找将字符串中指定字符删除,之后返回列表 split()

#split()与rsplit()功能相似,split为从左数,如不给可选参数则两函数功能相同
a = 'hello'
b = a.split('l',1)
print(b)

3.41  分离每一行内容 splitlines()

#splitlines()以换行符为分隔符分离字符串,默认参数为False,保留换行符
#splitlines含义为split lines
a = 'hello\nworld'
b = a.splitlines()
print(b)

c = 'hello\nworld'
d = a.splitlines(False)
print(d)

#如将参数置为True则保留换行符
e = 'hello\nworld'
f = a.splitlines(True)
print(f)

#三引号字符串换行同样适用
g = '''hello
world
'''
h = g.splitlines()
print(h)

3.42  判断起始字符串是否为指定字符 startswith()

#startswith()与endswith()用法相似,startswith()为检测字符串开头字符是否为指定字符
#可通过可选参数设置起始点与终止点
a = 'hello'
b = a.startswith('h')
print(b)

c = 'hello'
d = a.startswith('h',1,2)
print(d)

3.43  删除两侧的指定字符 strip() 

 咱们有从左删lstrip().从右删rstrip(),两侧删strip()

#strip()函数为删除前后两侧的指定字符,默认为空格
a = '   hello   '
b = a.strip()
print(b)

c = 'ohello'
d = c.strip('o')
print(d)

3.44  小写变大写,大写变小写 swapcase() 

#swapcase()函数功能为将字符串中所有大小写进行转换
a = 'hello world'
b = a.swapcase()
print(b)

c = 'HELLO WORLD'
d = c.swapcase()
print(d)

c = 'HelLo worLD'
d = c.swapcase()
print(d)

3.45  将每一个单词的首字母变为大写字母 title()

 与capitalize()的区别是capitalize()只变第一个单词,title()是所有单词都变

#title()功能为将字符中的首字母转变为大写字母
a = 'hello world'
b = a.title()
print(b)

3.46  设置字符对应maketrans()与替换对应字符translate()

#translate()函数需配合maketrans()函数同时使用
#maketrans()函数制作字符对应其他字符的tab表,translate()函数进行字符替换
intab = "eo"
outtab = "09"
trantab = intab.maketrans(intab, outtab)

a = 'hello world'
b = a.translate(trantab)
print(b)

上面的过程常用于加密与解密,我们下面来搞一个例子 

我们现在搞一个字母与数字混合的加密

Plaintext_list = []
for a in range(65,91):
    a = chr(a)
    Plaintext_list.append(a)
print(Plaintext_list)
print()

for a in range(97,123):
    a = chr(a)
    Plaintext_list.append(a)
print(Plaintext_list)
print()

for a in range(0,10):
    a = str(a)
    Plaintext_list.append(a)
print(Plaintext_list)

现在我们有大写A-Z,小写a-z,数字0-9的列表

我们现在将其乱序作为其映射

import random
ciphertext_list = Plaintext
random.shuffle(ciphertext_list)
print(ciphertext_list)

 生成明文字符串

Plaintext_test = ''
for a in Plaintext_list:
    Plaintext_test = Plaintext_test + a
print(Plaintext_test)

生成密文字符串 

ciphertext_test = ''
for a in ciphertext_list:
    ciphertext_test = ciphertext_test + a
print(ciphertext_test)

 加密

intab = Plaintext_test
outtab = ciphertext_test
encryption = intab.maketrans(intab, outtab)

a = 'hello world'
b = a.translate(encryption)
print(b)

 解密

intab = Plaintext_test
outtab = ciphertext_test
decryption = intab.maketrans(outtab, intab)

a = 'wJKK4 f4HKd'
b = a.translate(decryption)
print(b)

intab = Plaintext_test
outtab = ciphertext_test
encryption = intab.maketrans(intab, outtab)

a = 'tianjin jinnanqu tianjindaxue beiyangxiaoqu'
b = a.translate(encryption)
print(b)

intab = Plaintext_test
outtab = ciphertext_test
encryption = intab.maketrans(intab, outtab)

a = 'shanghai xiafeilu 32hao'
b = a.translate(encryption)
print(b)

 我们以上是没更换密码本的,我们可以每次口令都一样,然后每一个产品使用不同的密码本以达到加密的方式

3.47  将所有内容改为大写 upper()

#upper()将字符串中所有小写字母转变为大写字母
a = 'hello world'
b = a.upper()
print(b)

3.48  指定字符长度后之后用字符0填充 zfill() 

#zfill()使用字符0填充指定字符长度
a = 'hello'
b = a.zfill(10)
print(b)

a = 'hello'
b = a[2:]
print(b)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Suyuoa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值