Nicole37 发表于 2023-10-18 16:20:13

零py-(ep-21)

本帖最后由 Nicole37 于 2023-10-18 16:20 编辑

字符串4:
截取字符串
strip()、lstrip()、rstrip():截取掉空白或按单个字符遍历的截取
removeprefix(prefix),removesuffix(suffix):截取掉前缀和后缀的子字符串
#字符串的截取
#strip()表示跳过空白的地方
#lstrip()表示从左侧跳过空白的地方
#rstrip()表示从右侧跳过空白德地方
"   how are you doing?".lstrip()
'how are you doing?'
"how are you doing?      ".rstrip()
'how are you doing?'
"      how are you doing?      ".strip()
'how are you doing?'
#当strip中有参数时,表示从某一侧或者从两侧依次查找字符串,一一查找,发现有符合的就直接删掉
"which do you like the most in Friends?".lstrip("whi")
'ch do you like the most in Friends?'
"which do you like the most in Friends?".rstrip("Fri")
'which do you like the most in Friends?'
"which do you like the most in Friends?".strip("whiends")
'ch do you like the most in Friends?'
#想要踢掉具体的子字符串:removeprefix(prefix),removesuffix(ssuffix)允许指定将要删除的前缀或后缀,prefix:前缀,suffix:后缀
"well,i think so".removeprefix("well")
',i think so'
"well,i think so".removesuffix("so")
'well,i think '
拆分&拼接:
拆分:
partition(sep)、rpartition(sep)将字符串以参数指定的分隔符为依据进行切割,并将切割后的结果返回一个三元组,三个元素的元祖
split("参数",分割次数)、rsplit("参数","分割次数")不填参数,默认按空格切分;或通过参数来指定分割符
splitlines()将字符串按行进行分割,将结果以列表的形式返回
#字符串的拆分
#partition(sep)、rpartition(sep)将字符串以参数指定的分隔符为依据进行切割,并将切割后的结果返回一个三元组,三个元素的元祖,rpartition从右到左进行查找分割,partition从左到右进行分割
#找到第一个分割点,分割点的左面,分割点,分割点的右面,三部分
"www.ilovefishc.com".removeprefix(".")
'www.ilovefishc.com'
"www.ilovefishc.com".partition(".")
('www', '.', 'ilovefishc.com')
"i/lobedaujo/ddd".rpartition("/")
('i/lobedaujo', '/', 'ddd')
#split()不填参数,默认按空格切分;或通过参数来指定分割符
#split()不填参数,默认按空格切分;或通过参数来指定分割符
"i really dont have any plan about what to do".split()
['i', 'really', 'dont', 'have', 'any', 'plan', 'about', 'what', 'to', 'do']
"what,is,wrong,with,you".split(",")
['what', 'is', 'wrong', 'with', 'you']
#split("参数",分割次数)、rsplit("参数","分割次数")
"hello hotguy what do you do?".split(,1)
SyntaxError: invalid syntax
"hello hotguy what do you do?".split( ,1)
SyntaxError: invalid syntax
"hello hotguy what do you do?".split(" ",1)
['hello', 'hotguy what do you do?']
"hello hotguy what do you do?".rsplit(" ",1)
['hello hotguy what do you', 'do?']
#splitlines()将字符串按行进行分割,将结果以列表的形式返回
#不同操作系统之间表示换行的转义字符不同,/n,/r
"ok\nlisten\n".splitlines()
['ok', 'listen']
"ok\rlisten\r".splitlines()
['ok', 'listen']
#splitlines()包含一个keepends参数,指定结果是否要包含这个换行符,如果是true那就包含这个换行符,默认false表示不包含
"ok\nlisten\n".splitlines(True)
['ok\n', 'listen\n']

字符串拼接:
用join
#字符串的拼接:join(iterable)
#前面为分割符,后面是待拼接的字符串
".".join(["i","am","good"])
'i.am.good'
#也可以用元祖来承接待拼接的子字符串
"/".join(("what","kind","of","job","do","you","want"))
'what/kind/of/job/do/you/want'
#join与+ 的区别
s="wow"+"hot"
s
'wowhot'
"".join(["wow","hot"])
'wowhot'
#join的拼接效率更快
作业:
动手0:
a = input("请输入需要加密的明文密码(words only):")
b = input("请输入移动的位数")
c = a.split()
e=len(c)
d = []
h=0
base = ord('a')
base1 = ord('A')
for i in c:
    for j in range(len(i)):
      if i.isupper():
            d.append(chr((ord(i)-base1+int(b))%26+base1))
      if i.islower():
            d.append(chr((ord(i)-base+int(b))%26+base))
    d.append(",")
y = " ".join(d)
print(y.replace(","," "))
作业答案:
0:a = input("请输入明文密码:")
b = input("请输入移动位数")
base1 = ord('a')
base2 = ord('A')
c=[]
for i in a:#不需要把a分成小字符串,直接一个一个看就好,空格直接赋进去
    if i==" ":
      c.append(" ")
    else:
      if i.isupper():
            base = base2
      else:
            base = base1
      c.append(chr((ord(i)-base+int(b))%26+base))#(取待转换的字符串的ascii—a/A的ascii+转移的个数)%26+a/A的ascii
print(''.join(c)) #将列表转化为字符串,因为前面有空格,不需加其他任何的分割符
1:words = ["Twitter", "TOTO", "FishC", "Python", "ASL"]
a=[]
for i in words:
    j=i.lower() #大小写不一致,先进行转换
    if j.strip("qwertyuiop")=="" or j.strip("asdfghjkl")=='' or j.strip("zxcvbnm")=='':
      a.append(i) #利用strip()截取,从字符串的两头一一查找,如果有()内的字母,则删除掉,如果删除,则只剩下空
print(a)

如果要从字符串 "https://ilovefishc.com/html5/index.html" 中提取出 "ilovefishc.com",使用 split() 方法应该如何实现呢?
"https://ilovefishc.com/html5/index.html".split('//').split('/')
'ilovefishc.com'
注意:用了列表的子字符串选取
页: [1]
查看完整版本: 零py-(ep-21)