大鹏

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

字符串的用法

 

1、要需要掌握的操作

#1、strip,lstrip,rstrip
#2、lower,upper
#3、startswith,endswith
#4、format的三种玩法
#5、split,rsplit
#6、join
#7、replace
#8、isdigit
#strip
name = ' *egon** '
print(name.strip())       #*egon**   默认是去除空格
print(name.lstrip())   #*egon**   去左边空格
print(name.rstrip())   # *egon**  去右边空格

name = '*egon**'
print(name.strip("*"))    #egon

#lower,upper
name ='egon'
print(name.upper())
print(name.lower())

##startswith,endswith
name ='sdf_SB'
print(name.endswith("SB"))     #True
print(name.startswith('sdf'))  #True

#format的三种玩法
res = '{} {} {}'.format('egon',18,'male')
print(res)   #egon 18 male

res1 ='{1} {0} {1}'.format('egon',18,'male')
print(res1)    #18 egon 18

res2 = '{name} {age} {sex}'.format(sex='male',name='egon',age=18)
print(res2)    #egon 18 male

"""  """

# split
name = 'root:x:0:0::/root:/bin/bash'
print(name.split(':')) # 默认分隔符为空格   # ['root', 'x', '0', '0', '', '/root', '/bin/bash']


name='C:/a/b/c/d.txt' # 只想拿到顶级目录
print(name.split('/',1))     # ['C:', 'a/b/c/d.txt']

name = 'a|b|c'
print(name.rsplit('|',1)) # 从右开始切分 # ['a|b', 'c']

#join
tag=' '
print(tag.join(['egon','say','hello','world'])) # 可迭代对象必须都是字符串
# 结果: egon say hello world

# replace
name = 'alex say :i have one tesla,my name is alex'
print(name.replace('alex','SB',1))
# SB say :i have one tesla,my name is alex

# isdigit:可以判断bytes和unicode类型,是最常用的用于于判断字符是否为"数字"的方法
age=input('>>: ')
print(age.isdigit()) # >>:
代码表达1

 

2、isdigit() 、isalnum() 、isalpha()三者的用法

name7.isalnum()   # 字符串由字母或数字组成,字符串长度大于1
name7.isalpha()   # 字符串只由字母组成 ,字符串长度大于1
name.isdigit()    # 字符串只由数字组成,字符串长度大于1
#is数字系列
#在python3中
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='' #中文数字
num4='' #罗马数字

#isdigt:bytes,unicode
print(num1.isdigit()) #True
print(num2.isdigit()) #True
print(num3.isdigit()) #False
print(num4.isdigit()) #False

name='2'
print(name.isdigit())  # 判断是否是数字 # True

name4="3a"
print(name4.isalnum())  # 字符串由字母或数字组成 # True
print(name4.isalpha())  # 字符串只由字母组成 #False

name5 = "aadADa"
print(name5.isalpha())  # 字符串只由字母组成 #True
print(name5.isalnum())  # 字符串由字母或数字组成 # True

name6 = "aadADa1"
print(name6.isalpha())  # 字符串只由字母组成 # False


name7="333"
print(name7.isalnum())  # 字符串由字母或数字组成 # True
print(name7.isalpha())  # 字符串只由字母组成 #False

name7.isalnum()   # 字符串由字母或数字组成,字符串长度大于1
name7.isalpha()   # 字符串只由字母组成 ,字符串长度大于1
name.isdigit()    # 字符串只由数字组成,字符串长度大于1
代码表达2

 

3、split

name3.split(sep=None,maxsplit=-1)  # 不带参数默认是以空格分隔符切片字符串,
# 如果maxsplit参数有设置,则仅分隔maxsplit个子字符串,返回切片后的子字符串拼接的列表
#split
name1 = 'root:x:0:0:: /root:/bin/bash'
print(name1.split(' '))  # 按空格分 ['root:x:0:0::', '/root:/bin/bash']

name2 = 'root :x:0:0::  /root: /bin /bash'
print(name2.split())  # 默认按空格分 #['root', ':x:0:0::', '/root:', '/bin', '/bash']

name3 = 'root:x:0:0::/root:/bin/bash'
print(name3.split(':'))  # 默认分隔符为空格   # ['root', 'x', '0', '0', '', '/root', '/bin/bash']

name3.split(sep=None,maxsplit=-1)  # 不带参数默认是以空格分隔符切片字符串,
# 如果maxsplit参数有设置,则仅分隔maxsplit个子字符串,返回切片后的子字符串拼接的列表

print(name3.split(":",maxsplit=1))   # ['root', 'x:0:0::/root:/bin/bash']
print(name3.split(":",maxsplit=2))   # ['root', 'x', '0:0::/root:/bin/bash']
print(name3.split(":",maxsplit=3))   # ['root', 'x', '0', '0::/root:/bin/bash']
print(name3.split(":",maxsplit=4))   # ['root', 'x', '0', '0', ':/root:/bin/bash']
代码表达3

 

4.count

def strBeauty(s):
    beauty = 0
    dict1 = {}
    # 字典放名字中每种字母对应出现到次数
    for a in set(s):
        k = s.count(a)
        dict1[a] = k
    # print(dict1)

    # 每种字母的出现次数从大到小排列
    time_list = sorted(dict1.values(), reverse=True)
    # print(time_list)

    # 次数从大到小以此乘以26,25,24...
    for i in range(len(time_list)):
        beauty += (26 - i) * time_list[i]
    return beauty


while True:
    try:
        n = int(input())
        sa, sb = input(), input()
        print(strBeauty(sa))
        print(strBeauty(sb))
    except:
        break
代码表达1

 

 

 

 

   

posted on 2022-05-10 23:19  pf42280  阅读(120)  评论(0)    收藏  举报