zoukankan      html  css  js  c++  java
  • python-day7-字符串类型的内置方法

    # name='egon' #name=str('egon')
    # print(type(name))


    #优先掌握
    #    移除空白strip
    # msg=' hello '
    # print(msg)
    # print(msg.strip())

    # msg='***hello*********'
    # msg=msg.strip('*')
    # print(msg)

    # print(msg.lstrip('*')) #lstrip是已移除首部分的*
    # print(msg.rstrip('*')) #rstrip是已移除尾部分的*

    #用处
    # while True:
    # name=input('user: ').strip()
    # password=input('password: ').strip()
    #
    # if name == 'egon' and password == '123':
    # print('login successfull')



    #     切分split
    # info='root:x:0:0::/root:/bin/bash'
    # print(info[0]+info[1]+info[2]+info[3])

    # user_l=info.split(':')
    # print(user_l[0])

    # msg='hello world egon say hahah'
    # print(msg.split()) #默认以空格作为分隔符

    # cmd='download|xhp.mov|3000'
    # cmd_l=cmd.split('|')
    # print(cmd_l[1])
    # print(cmd_l[0])

    # print(cmd.split('|',1))

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


    # 用处
    # while True:
    # cmd=input('>>: ').strip()
    # if len(cmd) == 0:continue
    # cmd_l=cmd.split()
    # print('命令是:%s 命令的参数是:%s' %(cmd_l[0],cmd_l[1]))




    #     长度len

    # print(len('hell 123'))


    #     索引


    #     切片:切出子字符串
    # msg='hello world'
    # print(msg[1:3]) #1 2
    # print(msg[1:4]) #1 2 3



    # 掌握部分
    # oldboy_age=84
    # while True:
    # age=input('>>: ').strip()
    # if len(age) == 0:continue
    # if age.isdigit():
    # age=int(age)
    # else:
    # print('must be int')





    #startswith,endswith
    # name='alex_SB'
    # print(name.endswith('SB'))
    # print(name.startswith('alex'))


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

    # print('my name is %s my age is %s my sex is %s' %('egon',18,'male'))

    #format的三种玩法
    # print('my name is {} my age is {} my sex is {}'.format('egon',18,'male'))
    # print('my name is {0} my age is {1} my sex is {0}:{2}'.format('egon',18,'male'))
    # print('my name is {name} my age is {age} my sex is {sex}'.format(
    # sex='male',
    # age=18,
    # name='egon'))


    # #find,rfind,index,rindex,count
    # name='geeon say hello'
    # print(name.find('S',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
    # print(name.index('e')) #同上,但是找不到会报错,如果不指定范围则查找所有
    # print(name.count('e',1,5)) #顾头不顾尾,如果不指定范围则查找所有

    #join
    # info='root:x:0:0::/root:/bin/bash'
    # print(info.split(':'))

    # l=['root', 'x', '0', '0', '', '/root', '/bin/bash']
    # print(':'.join(l)) #把 : 加入到 l 列表

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



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






    #了解部分
    #expandtabs
    # name='egon hello'
    # print(name)
    # print(name.expandtabs(1))


    #center,ljust,rjust,zfill
    # name='egon'
    # # print(name.center(30,'-'))
    # print(name.ljust(30,'*'))
    # print(name.rjust(30,'*'))
    # print(name.zfill(50)) #用0填充


    #captalize,swapcase,title
    # name='eGon'
    # print(name.capitalize()) #首字母大写,其余部分小写
    # print(name.swapcase()) #大小写翻转
    # msg='egon say hi'
    # print(msg.title()) #每个单词的首字母大写




    # #is数字系列
    #在python3中
    # num0='4'
    # num1=b'4' #bytes
    # num2=u'4' #unicode,python3中无需加u就是unicode
    # num3='四' #中文数字
    # num4='Ⅳ' #罗马数字

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

    #isdecimal:str,unicode
    # num0='4'
    # num1=b'4' #bytes
    # num2=u'4' #unicode,python3中无需加u就是unicode
    # num3='四' #中文数字
    # num4='Ⅳ' #罗马数字
    # print(num0.isdecimal()) #True
    # # print(num1.)
    # print(num2.isdecimal()) #True
    # print(num3.isdecimal()) #False
    # print(num4.isdecimal()) #False

    #isnumeric:str,unicode,中文数字,罗马数字
    # num0='4'
    # num1=b'4' #bytes
    # num2=u'4' #unicode,python3中无需加u就是unicode
    # num3='四' #中文数字
    # num4='Ⅳ' #罗马数字
    #
    # print(num0.isnumeric()) #True
    # # print(num1)
    # print(num2.isnumeric()) #True
    # print(num3.isnumeric()) #True
    # print(num4.isnumeric()) #True

    # #三者不能判断浮点数
    # num5='4.3'
    # print(num5.isdigit())
    # print(num5.isdecimal())
    # print(num5.isnumeric())
    # '''
    # 总结:
    # 最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景
    # 如果要判断中文数字或罗马数字,则需要用到isnumeric
    # '''


    #is其他
    # name='egon123'
    # print(name.isalnum()) #字符串由字母和数字组成
    # name='asdfasdfa sdf'
    # print(name.isalpha()) #字符串只由字母组成

    # name='asdfor123'
    # print(name.isidentifier()) #True
    # name='egGon'
    # print(name.islower()) #False
    # # print(name.isupper()) #False
    # # print(name.isspace()) #False
    # name='Egon say'
    # print(name.istitle()) #False
  • 相关阅读:
    git npm包管理
    c# 多线程多个参数
    c# 笔试面试题01
    数据笔试题
    SQL重复记录查询的几种方法
    大数据库脚本文件执行
    ef5 数据库操作
    nodejs ejs模板数据库操作
    node jade模板数据库操作
    nodejs 中jead模板改为ejs
  • 原文地址:https://www.cnblogs.com/liuwei0824/p/7206934.html
Copyright © 2011-2022 走看看