• python全栈开发从入门到放弃之字符串的应用


    1、strip

    strip 脱去(...的)衣服

    去掉左右两边的空白msg=' hellprint(msg)

     1  1 print(msg.strip()) #去掉左右两边的空白
     2  2 
     3  3 hello
     4  4 hello
     5  5 
     6  6 msg='********hello************'
     7  7 print(msg)
     8  8 print(msg.strip('*')) #指定的去掉左右两边的*
     9  9 ********hello************
    10 10 
    11 11 hello
    12 12 (1)lstrip 指定去掉左边的*
    13 13 # print(msg.lstrip('*'))
    14 14 msg='********hello************'
    15 15 print(msg)
    16 16 print(msg.lstrip('*')) #指定去掉左边的*
    17 17 ********hello************
    18 18 hello************
    19 19 
    20 20 (2)rstrip 指定去掉右边的连续的*
    21 21 # print(msg.rstrip('*'))
    22 22 msg='********hello************'
    23 23 print(msg)
    24 24 print(msg.rstrip('*')) #指定去掉右边的连续的*
    25 25 ********hello************
    26 26 ********hello

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

     2、split 切分

     1 1 info='root:x:0:0::/root:/bin/bash'
     2 2 print(info[0]+info[1]+info[2]+info[3])
     3 3 root
     4 
     5 info='root:x:0:0::/root:/bin/bash'
     6 print(info[0]+info[1]+info[2]+info[3])
     7 
     8 info='root:x:0:0::/root:/bin/bash'
     9 print(info[0]+info[1]+info[2]+info[3])
    10 info='root:x:0:0::/root:/bin/bash'
    11 print(info[0]+info[1]+info[2]+info[3])
    12 
    13 user_l=info.split(':') #用内置方法以‘:’能把字符串类型以列表的形式呈现出来,来取值
    14 print(user_l[0])
    15 
    16 
    17 # msg='hello world egon say hahah'
    18 # print(msg.split()) #默认以空格作为分隔符
    19 msg='hello world egon say hahah'
    20 print(msg.split()) #默认以空格为分隔符
    21 ['hello', 'world', 'egon', 'say', 'hahah']
    22 
    23 
    24 
    25 
    26 cmd='download|xhp.mov|3000'
    27 # cmd_l=cmd.split('|')
    28 # print(cmd_l[1])
    29 # print(cmd_l[0])
    30 
    31 # print(cmd.split('|',1))

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



    3、len 长度

    1 1 # print(len('hell 123'))
    2 2 print(len('hell 123')) #查看字符串的长度
    3 8   #字符串长度为8



    4、索引

    根据索引来取出子字符串

    1 msg='hello world
    2 print(msg[1:3]) #1 2
    3 print(msg[1:4]) #1 2 3
    4 msg='hello world'
    5 print(msg[1:3]) #切出字符串子集但顾头不顾尾
    6 el

    5、混合应用

    1 oldboy_age=84
    2 while True:
    3 age=input('>>: ').strip()
    4 if len(age) == 0:continue
    5 if age.isdigit():
    6 age=int(age)
    7 else:
    8 print('must be int')

    6、endswith

    1 #startswith,endswith
    2 # name='alex_SB'
    3 # print(name.endswith('SB')) #查找字符串结尾里是否有输入的参数,有着返回True,没有返回False
    4 # print(name.startswith('alex')) #查找开头是否有输入的参数,有着返回True,没有返回False

    7、replace

    1 #replace
    2 # name='alex say :i have one tesla,my name is alex'
    3 # print(name.replace('alex','SB',1))
    4 name = 'alex say :i have one tesla,my name is alex'
    5 print(name.replace('alex','SB',1)) #在开头和结尾添加指定字符
    6 SB say :i have one tesla,my name is alex

    8、占位符

     1 # %s对应所有字符 一一对应后面的元素 
     2 print('my name is %s my age is %s my sex is %s' %('egon',18,'male'))
     3 #把{}当作一个占位符,.format调用对应每一个后面输入的字符
     4 print('my name is {} my age is {} my sex is {}'.format('egon',18,'male'))
     5 my name is egon my age is 18 my sex is male
     6 #根据{}里的索引来对应值,当{}没有值时默认为一一对应。
     7 print('my name is {0} my age is {1} my sex is {0}:{2}'.format('egon',18,'male'))
     8 my name is egon my age is 18 my sex is egon:male
     9 #根据{}里的输入的key来准确调用key对应的元素
    10 print('my name is {name} my age is {age} my sex is {sex}'.format(
    11 sex='male',
    12 age=18,
    13 name='egon'))
    14 my name is egon my age is 18 my sex is male

    9、find、index、count查找

     1 #可以指定范围查找但顾头不顾尾,找到则返回该值的索引号,没有则返回-1不报错,
     2 # find 查找
     3 name='goee say hello'
     4 print(name.find('e',1,3)) 
     5 #index 查找 
     6 可以指定范围查找但顾头不顾尾,找到则返回该字符串的索引号,没有则报错
     7 name='goee say hello'
     8 print(name.index('s'))
     9 
    10 #count 
    11 指定参数,查找字符串里的元素,有则返回这个查找的值里有多少数,有1个就返回1,2个就返回2 等等,没有则返回0
    12 name='aaaaaaaa aaaaa aaa'
    13 print(name.count('a'))
    14 16

    10、join

     1 #join
     2 # info='root:x:0:0::/root:/bin/bash'
     3 # print(info.split(':'))
     4 
     5 # l=['root', 'x', '0', '0', '', '/root', '/bin/bash']
     6 # print(':'.join(l))
     7 
     8 #join 两个物体连接处
     9 指定分隔符默认,用:连接起来
    10 l=['root', 'x', '0', '0', '', '/root', '/bin/bash']
    11 print(':'.join(l))
    12 root:x:0:0::/root:/bin/bash

    11、lower,upper大小写转换

     1 #lower,upper
     2 # name='eGon'
     3 # print(name.lower())
     4 # print(name.upper())
     5 将大写字母全转为小写字母
     6 name = 'Egon'
     7 print(name.lower())
     8 egon
     9 
    10 将小写字母全转为大写
    11 name = 'Egon'
    12 print(name.upper())

    了解部分

    1、expandtabs 指定参数多少个空格符

    1 name='egon	hello'
    2 print(name)
    3 print(name.expandtabs(3))
    4 egon    hello
    5 egon hello

    2、center,ljust,rjust,zfill

     1 以原有的变量的字符为中心两边添加指定数量的符号
     2 name='egon'
     3 print(name.center(30,'_'))
     4 _____________egon_____________
     5 
     6 ljust 
     7 原有元素左对齐后面添加指定数量的符号
     8 name='egon'
     9 print(name.ljust(30,'*'))
    10 egon**************************
    11 rjust
    12 原有元素右对齐,往左边添加指定数量的符号
    13 name='egon'
    14 print(name.rjust(30,'*'))

    3、captalize,swapcase,title

     1 captalize
     2 首字母大写,其余部分小写
     3 name='eGOn'
     4 print(name.capitalize())
     5 Egon
     6 
     7 title
     8 每个单词的首字母大写
     9 msg='egon say hi'
    10 print(msg.title())
    11 
    12 swapcase
    13 小写互相对调大写变小写,小写变大写
    14 name='eGOn'
    15 print(name.swapcase())
    16 EgoN

    #在python3中

    4、元素判断

     1  1 isdigit 是否为数字
     2  2 判断字符串里是否为数字,是则返回True,不是则返回False
     3  3 
     4  4 num0='4'
     5  5 num1=b'4' #bytes
     6  6 num2=u'4' #unicode,python3中无需加u就是unicode
     7  7 num3='' #中文数字
     8  8 num4='' #罗马数字
     9  9 
    10 10 print(num0.isdigit())
    11 11 print(num1.isdigit())
    12 12 print(num2.isdigit())
    13 13 print(num3.isdigit())
    14 14 print(num4.isdigit())
    15 #isdecimal:str,unicode
    16 # num0='4'
    17 # num1=b'4' #bytes
    18 # num2=u'4' #unicode,python3中无需加u就是unicode
    19 # num3='四' #中文数字
    20 # num4='Ⅳ' #罗马数字
    21 # print(num0.isdecimal())
    22 # # print(num1.)
    23 # print(num2.isdecimal())
    24 # print(num3.isdecimal())
    25 # print(num4.isdecimal())
    26 
    27 isdecimal
    28 判断一个数是否为十进制数,是则返回True,不是则返回False
    29 num0='4'
    30 num1=b'4'
    31 num2=u'4'
    32 num3=''
    33 num4=''
    34 print(num0.isdecimal())
    35 print(type(num1))
    36 print(num2.isdecimal())
    37 print(num3.isdecimal())
    38 print(num4.isdecimal())
    39 True
    40 <class 'bytes'>
    41 True
    42 False
    43 False
    44 
    45 
    46 #isnumeric:str,unicode,中文,罗马
    47 # num0='4'
    48 # num1=b'4' #bytes
    49 # num2=u'4' #unicode,python3中无需加u就是unicode
    50 # num3='四' #中文数字
    51 # num4='Ⅳ' #罗马数字
    52 #
    53 # print(num0.isnumeric())
    54 # # print(num1)
    55 # print(num2.isnumeric())
    56 # print(num3.isnumeric())
    57 # print(num4.isnumeric())
    58 
    59 isnumeric 同质异构的
    60 只针对与unicode字符的判断是否为数字,是则为真不是则为假
    61 num0='4'
    62 num1=b'4'
    63 num2=u'4'
    64 num3=''
    65 num4=''
    66 print(num0.isnumeric())
    67 print(type(num1))
    68 print(num2.isnumeric())
    69 print(num3.isnumeric())
    70 print(num4.isnumeric())
    71 True
    72 <class 'bytes'>
    73 True
    74 True
    75 True

    5、is其他 isalnum、isalpha

     1 # name='egon123'
     2 # print(name.isalnum()) #字符串由字母和数字组成
     3 # name='asdfasdfa sdf'
     4 # print(name.isalpha()) #字符串只由字母组成
     5 #
     6 isalnum 字符测试
     7 测试变量里的元素是否为字母和数字组成
     8 name = 'egon123'
     9 print(name.isalnum())
    10 True
    别想一下造出大海,必须先由小河川开始。
  • 相关阅读:
    Three.js 类的粗略总结和实现
    JavaScript方法
    JavaScript 继承和数组
    JavaScript 函数
    操作系统之堆和栈的区别
    第十二章 动态内存
    第十三章-第六小节-对象移动
    第十五章 面形对象程序设计
    动态建树和静态建树
    高斯消元整数版和浮点数版实现
  • 原文地址:https://www.cnblogs.com/zcfx/p/7220820.html
Copyright © 2020-2023  润新知