zoukankan      html  css  js  c++  java
  • 字符串

    字符串是python中最常见的数据类型,可以用双引号或者单引号来创建

    a = "python"
    b = 'python'

    字符串常用方法

    find() 方法     和rfind方法

    str.find('要查找的内容',开始的地方,结束的地方)

    查找字符串的下标

    如果没有找到就返回   -1 

    rfind 和 find 作用是一样的   只不过是从右边开始查找

    a = 'asdfghjkl'
    print(a.find('j'),0,10)   # 6
    print(a.rfind('j')) # 2

    index()方法 和 rindex()方法

    和find方法作用一样,都是要查找字符串

    str.index('要查找的内容')

    返回他所在的下标

    找不到就会报错

    可以用try异常来捕捉

    rindex 和 index一样只不过是从右边开始

    a = 'asdfghjklqwertyuiop'
    try:
        print(a.index('y'))
    except Except as e:
        print(-1)
    触发异常返回-1

     count()   方法

    查询字符串出现的次数

    str.count('要查找的内容')

    没有就返回0

    a = 'aaabbbbccccdddddiiiiiiiiii'
    ptint(a.count('a'))  # 3

    len()方法

    len(str)

    计算字符串长度

    mystr = "str"
    print(len(mystr))       # 3

    replace() 方法

    str.replace('旧的',‘新的’,替换的次数)

    把字符串替换成其他字符串

    a = 'hello word python'
    print('hello','HELLO',1)   # HELLO word python

    split()  方法

    字符串切片

    str.split('切割的字符')

    a = 'hello word python'
    print(a.split(' '))    # 以空格切分   'hello' 'word' 'python'

    capitalize() 方法

    str.capitalize()

    整个首字母大写

    a = 'hello word python'
    print(a.capitalize())  # Hello word python 

    title()方法

    str.title()

    每个单词首字母大写

    a = 'hello word python'
    print(a.title())  # Hello Word Python

    istitle()  方法

    str.istitle() 

    判断每个单词是否首字母大写

    a = 'Hello Word Python'
    print(‘a.istitle()’)     # true

    startswith()   方法  和   endswith()  方法

    str.startswith(需要判断的字符)

    str.endswith(需要判断的字符)

    判断字符串是否以某个字符作为开头

    endswith()   和   startswith()   作用一样  endswith是判断是否结尾为某个字符

    a = 'python'
    print(a.startswith('p'))   # true

     lower()方法     和     upper() 方法

    str.upper()

    str.lower()

    upper()   把所以字母大写

    lower()    把所有字母都变成小写

    a = 'HELLO WORD PYTHON'
    print(a.lower())     # hello word python
    a = 'hello word python'
    print(a.upper()) # HELLO WORD PYTHON

     ljust()  方法   、    rjust() 方法     和     center()    方法

    str.ljust(指定的长度,‘指定的字符’)     str.rjust(指定的长度,‘指定的字符’)     str.center(指定的长度,‘指定的字符’)    

    ljust     指定一个长度使用指定的字符填充最后居左

    rjust     指定一个长度使用指定的字符填充最后居右

    center     指定一个长度使用指定的字符填充最后居中

    mystr = "You got a dream"
    mystr.ljust(50,'*') # You got a dream***********************************
    mystr = 'You got a dream'
    mystr.rjust(50,'*') # ***********************************You got a dream
    mystr = "You got a dream"
    mystr.center(50,'*') # *****************You got a dream******************

    lstrip()   、   rstrip()     和      strip()  方法      

    str.lstrip()      去除左边空格、换行符或者指定的字母    

    str.rstrip()      去除右边空格、换行符或者指定的字母

    str.strip()       去除两边的空格,换行古或者指定的字母

    mystr = ' You got a dream '
    mystr.rstrip() # 'You got a dream '
    mystr.lstrip(' m') # ' You got a drea'
    mystr.strip() # 'You got a drea'

    partition()   和   rpartition()   方法

    str.partition()          按照指定的字符切割字符串  返回一个元祖

    str.rpartition()         从右边开始按照指定的字符  返回一个元祖

    a = 'abcd'
    print(a.partition('c'))    # ('ab','c','d')
    a = 'abcdcd
    print(a.rpartition('c'))   # ('abcd','c','d')

    splitlines()   方法

    str.splitilines()

    按照        这个来分隔字符串    如果括号里面为false不包含换行符,如果括号中为true,保留换行符

    mystr = 'You got a
     dream'
    mystr.splitlines() # [' You got a', ' dream']
    mystr.splitlines(True) # [' You got a
    ', ' dream']

    isalnum()  方法

    str.isalnum()  

    判断字符串是否只由数字和字母组成  返回布尔值

    a = 'aaa3aaaa'
    print(a.isalnum())  # true
    a = 'aaa b sd 4'
    print(a.isalnum())  # false   空格不是数字或字母

    isdigit()   方法

    str.isdigit()

    判断字符串只有数字   返回布尔值

    a = '234567890'
    print(a.isdigit())  # true
    a = 'sfsdf234324'
    print(a.isdigit())  # false  

    isalpha()  方法

    str.isalpha()  

    判断字符串是否由字母组成

    a = 'jkdsafhdsklja'
    print(a.isalpha())   # true
    a = '3dfrdfs324'
    print(a.isalpha())   # false

     isspace()  方法

    str.isspace()

    判断字符串是否由空格组成

    a = '        '
    print(a.isspace())   # true
    a = 'dsafadsf     '
    print(a.isspace())   # false

     join()    方法

    str1.join(str2)

    将指定的字符将序列化成一个新的字符串

    a = 'a'
    b = 'bbbbbbb'
    print(a.join(b))    # babababababab


    字符串
    字符串下标
    a = “ 123456 ”
    下标:从左向右 从0 开始
    从右往左 从-1开始
    切片:和for差不多 切片不支持数字 回文数的时候是字符串
    a=“123456”
    print(s[3:4:1])起始:结束:步长
    下标还是从0 开始
    包含头不包含尾
    括号里面可以什么都没有但是必须有 :冒号
    起始还是从0 开始
    结束默认为全部
    当下标只有一个值的时候结束下标取值

    字符串的操作
    相加:
    s = "hello"
    a = 3
    # 字符串操作
    # 1. 字符串支持字符串之间相加,相当于字符串拼接
    c = s + str(a)
    print(c) # c: hello3
    字符串支持字符串与整数n相乘 相当于字符串复制很多遍
    字符串可以被for循环遍历 取值
    s= “ 123456 ”
    for i in s
    print(i)


    1字符串的查找 index rindex find rfind count
    用数字来显示位置
    index 的框架(str,strat,end)str是要查找的内容 start 是从哪里开始查起 end查到哪里结束
    index从左边开始查找, 找到第一个返回下标,不存在,报错
    rindex从右边开始查找, 找到第一个返回下标,不存在,报错
    在寻找的时候会找到寻找内容的前一个字输出前一个字从右边开始也是一样的
    r = s . index ( 查找的内容 )
    print ( r )
    find从左边开始查找, 找到第一个返回下标,不存在,返回-1
    rfind从右边开始查找, 找到第一个返回下标,不存在,返回-1
    r = s . find (" 查找内容 ")
    print(r)
    查找的时候如果要查找双引号那么就可以用单引号包裹
    2 count 统计特定字符出现的次数, 存在,返回次数 不存在, 返回0
    r = s . count ( " 查找内容 " )
    print(r)
    3 字符的替换 replace
    旧符号在前,新符号在后面
    可以在后面加数字限定要替换的次数
    s = " 1234567890 "
    r = s . replace ( " , " , " " ) 中间的那个

  • 相关阅读:
    sh执行脚本报错Syntax error: "(" unexpected
    Ubuntu源码编译安装PostgreSQL
    Linux解决Graphical installers are not supported by the VM
    pytest功能入门
    vscode配置远程开发环境失败_workbench.downloadResource
    花生壳新版 内网穿透教程
    同局域网内进行本机调试
    Windows10激活
    win server 2012安装Vmware tools
    windows实现超级隐藏用户
  • 原文地址:https://www.cnblogs.com/lishanglin/p/12392055.html
Copyright © 2011-2022 走看看