• Python str类型方法实例概述及常用方法——04


    字符串(str)

    str的方法

    首字母大写(capitalize)

    test = 'winsdom'
    v = test.capitalize()
    print(v)
    #结果是:
    v = 'Winsdom'

    所有字符变成小写,但这里casefold可以对应很多未知的小写(casefold,lower)

    test = 'WINSDOM'
    v1 = test.casefold()
    v2 = test.lower()
    print(v1,v2)
    #结果是:
    v1 = 'winsdom'
    v2 = 'winsdom'

    设置宽度,并将内容居中,这里20代指总长度,后面不填即用空白来填充,且只能给一个字符,可有可无(center)

    test = 'winsdom'
    v = test.center(20,'-')
    print(v)
    #结果是:
    v = '------winsdom-------'

    计算一下该字符在字符串里的个数(出现的次数),参数,5表示从第5个位置开始找,6表示从5开始到6结束(count)

    test = 'winsdom'
    v = test.count('o',5,6)
    print(v)
    #结果是:
    v = 1

    表示判断以什么什么结尾,以什么什么开始,并返回布尔值(endswith,startswith)

    test = 'winsdom'
    v1 = test.startswith('w')
    v2 = test.endswith('m')
    print(v1,v2)
    #结果是:
    v1 = True
    v2 = True

    将字符串中的占位符替换成指定的值(format)

    test = 'i am {name},age{a}'
    v = test.format(name = 'winsdom',a = 19)
    print(v)
    #结果是:
    v = 'i am winsdom,age19'

    另一种写法,这里要一 一对应,且是从0开始的

    test = 'i am {0},age{1}'
    v = test.format('winsdom',19)
    print(v)
    #结果是:
    v = 'i am winsdom,age19'

    判断字符串中是否只包含字符和数字,仅字母或仅数字也可以,若有其他的则返回False

    test = 'sad1233!#!@'
    v = test.isalnum()
    print(v)
    #结果是:
    v = False

    expandtabs 的用法 :这里填参数20表示的是以Tab为断句 填充满20个单位  类似表格的形式

    test = 'username	email	password
    zhangsan	zhangsan@qq.com	123456
    lisi	lisi@qq.com' 
           '	123456
    wangwu	wangwu@qq.com	123456
    '
    v = test.expandtabs(20)
    print(v)
    #结果是:
    username            email               password
    zhangsan            zhangsan@qq.com     123456
    lisi                lisi@qq.com         123456
    wangwu              wangwu@qq.com       123456

    判断是否是字母(isalpha)

    test = 'dasda'
    v = test.isalpha()
    print(v)
    #结果是
    v = True

    判断是否是数字的三种方法(isdecimal能判断十进制的,isdigit能判断多类型,isnumeric能判断中文的)

    test = '123'
    v = test.isdecimal()
    print(v)
    #结果是
    v = True
    test = ''
    v = test.isdigit()
    print(v)
    #结果是
    v = True
    test = ''
    v = test.isnumeric()
    print(v)
    #结果是
    v = True

    判断是否存在不可显示的内容( , 之类)返回bool值(isprintable)

    test = 'oiuas	dasr'
    v = test.isprintable()
    print(v)
    #结果是:
    v = False

    判断是否全部都是空格 返回bool(isspace)

    test = ' '
    v  = test.isspace()
    print(v)
    #结果是:
    v = True

    判断是否是标题(字符串中所有单子首字母是否大写)(istitle),将字符串转换成标题(title)

    test = 'my name is winsdom i am a student'
    v = test.istitle()
    print(v)
    #结果是:
    v = False
    
    v = test.title()
    print(v)
    #结果是:
    v = My Name Is Winsdom I Am A Student

    字符串的常用方法

    join的用法:
    将字符串中的每一个元素按照指定分隔符进行拼接
    test = '你是风儿我是沙'   
    t = '_'
    v = t.join(test)        # v = '_'.join(test)   
    print(v)
    #结果是:
    v = '你_是_风_儿_我_是_沙'
    spilt的用法:

     将字符串分割

    test = '你是 风儿我 是沙'
    v = test.split()
    print(v)
    #结果是:
    v = ['你是', '风儿我', '是沙']
    与spilt相关的方法
    test.partition() 
    test.rpartition()
    test.split()
    test.rsplit()
    test.splitlines()
    find的用法:

    从头往后找,找到第一个之后,获取其位置(find,index),可以填参数,如从第几个开始到第几个之间找,建议用find,因为找不到会显示-1,而index找不到时会报错

    test = 'winsdom'
    v1 = test.find('z')
    v2 = test.index('i')
    print(v1,v2)
    #结果是:
    v1 = -1
    v2 = 1
    strip的用法:
    去除左右空白  去掉	 
       也可以加参数 去除某个指定字符 优先最多匹配
    test = ' winsdom '
    v = test.strip()  
    print(v)
    #结果是:
    v = 'winsdom'

    与strip相关的方法

    test.lstrip()
    test.rstrip()


    upper、lower的用法:

    将字符串全部转换成大写或者全部转换成小写

    test1 = ' winsdom '
    test2 = 'WINSDOM'
    v1 = test1.upper()
    v2 = test2.lower()
    print(v1,v2)
    #结果是:
    v1 = 'WINSDOM'
    v2 = 'winsdom'

    replace的用法:

    替换字符串中的字符

    test = 'winsodm'
    v = test.replace('w','a')
    print(v)
    #结果是:
    v = 'ainsdom'

    字符串中的索引

    索引又叫下标,通过索引能将字符串中的字符找出来,索引都是从0开始的

    test = 'winsdom'
    v = test[0]
    print(v)
    #结果是:
    v = 'w'

    字符串中的切片

    通过学习索引,我们可以完成切片,实例里是从第0个索引开始切到3为止,但不包括3

    test = 'winsdom'
    v = test[0:3]
    print(v)
    #结果是:
    v = 'win'
    len
    获取当前字符串由几个字符组成
    test = 'winsdom'
    v = len(test)
    print(v)
    #结果是:
    v = 7
    range
    帮助创建连续的数字,通过设置步长来指定不连续
    v = range(10)
    
    v = [0,1,2,3,4,5,6,7,8,9]
    v = range(0,100,5) 

    补充:

    for循环

    for 变量名 in 字符串:      for循环
    变量名
    for i in range(10):
        print(i)
    #结果是
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    ps:字符串一旦创建就不可修改,一旦修改或者拼接,都会造成重新生成字符串

  • 相关阅读:
    (转)MyEclipse +Servlet
    Android: MediaRecorder start failed
    Android: 帮助找出内存泄漏的工具
    Node & Express: some tips
    MySQL: Create Master
    scp: useful commands
    MySQL: 打开binlog选项后无法重启MySQL
    IIS: 配置web.config解决Maximum request length exceeded错误
    MySQL: 让MySQL支持颜文字emoji
    Linux: 通过命令行上传文件到ftp服务器
  • 原文地址:https://www.cnblogs.com/winsdom/p/9055880.html
Copyright © 2020-2023  润新知