美文网首页Python 3.7入门篇
6.string类型(内置函数)

6.string类型(内置函数)

作者: HassGy | 来源:发表于2019-07-25 23:59 被阅读0次

    ☑(续)6.数据类型str

    👉注意,字符串为不可变的对象,所有对原来的字符串的操作均无效(没影响)

    常用的内置(函数)方法

    大小写转换

    1.capitalize():字符串第一个字符大写

    str1 = 'python很牛批'
    
    print(str1) #
    
    res = str1.capitalize()
    
    print(res)
    ----------------------------------------------
    Python很牛批
    

    2.upper():全部变为大写

    str2 = 'Python IS nice'
    res = str2.upper()
    print(res)
    -----------------------------------------------
    PYTHON IS NICE
    

    3.lower():全部变为小写

    str3 = 'Python IS nice'
    res = str3.lower()
    print(res)
    -----------------------------------------------
    python is nice
    

    4.swapcase():大小写互相转换

    str4 = 'Python IS nice'
    res = str4.swapcase()
    print(res)
    -----------------------------------------------
    pYTHON is NICE
    

    5.title():字符串中所有的单词都以大写开头,其余字母小写

    str5 = 'python is nice'
    res = str5.title()
    print(res)
    -----------------------------------------------
    Python Is Nice
    

    字符串的‘切’操作

    1.center(n, '字符'):返回长度为n的字符串,使原字符串居中,其余位置''字符''补全

    s1 = "周杰伦"
    
    res = s1.center(10, "*") 
    
    print(res)
    
    

    2.expandtabs(tabsize=4):把字符串中的tab转为空格,默认空格数为8,这里改为4

    s2 = "hassgy\tTom"
    
    res = s2.expandtabs()
    
    print(res) 
    
    

    3.strip():去除左右两边空格; 理解:lstrip(); rstrip()分布为去除左/去除右

    s3 = " hassgy Tom "
    
    res = s3.strip() 
    
    print(res)
    
    res = s3.lstrip() 
    
    print(res)
    
    res = s3.rstrip() 
    
    print(res)
    
    

    4.strip(str1):指定去除字符串str1

    s4 = " hassgy Tom "
    res = s4.strip('Tom')
    
    print(res)
    ----------------------------------------
    hassgy Tom
    
    

    5.replace('被替换字符串','替换字符串',n):理解为:替换n个被替换字符串。

    s5 = "Hassgy loves Amy."
    
    res = s5.replace('loves', 'hates') 
    
    print(s5) 
    
    print(res) 
    ----------------------------------------
    Hassgy loves Amy.
    
    Hassgy hates Amy.
    ----------------------------------------
    res = s5.replace('s', 'S', 2)
    print(res)
    ----------------------------------------
    HaSSgy loves Amy.
    
    

    6.split('切割标志'):字符串切割,比如根据","切割原字符串,还有其他的类型。

    s6 = "hassgy,Tom,Amy,sarah"
    
    res = s6.split(",")
    
    print(res)
    
    s7 = """辣鸡
    辣鸡
    辣鸡"""
    
    print(s7.split("\n"))
    ------------------------------------
    ['hassgy', 'Tom', 'Amy', 'sarah']
    
    ['辣鸡', '辣鸡', '辣鸡']
    
    

    👉⚠巨坑:如果切割标志在左右两端,那就会出现空字符串,请记住!!!

    s8 = ', hassgy, Tom, Amy, '
    res = s8.split(',')
    print(res)
    -------------------------------------
    ['', ' hassgy', ' Tom', ' Amy', ' ']
    
    

    格式化输出(字符串)

    1.(%s,......) % (值1,值2,......) 基础写法

    s1 = "我叫%s, 今年%d岁了, 我喜欢%s" % ('Hassgy', 15, 'Amy') 
    
    print(s1)
    --------------------------------------------------------------------
    我叫Hassgy, 今年15岁了, 我喜欢Amy
    
    

    2.''{}...''.format(值1,值2,......) 位置格式化

    s2 = "我叫{}, 今年{}岁了, 我喜欢{}".format("Hassgy", 15, "Amy") 
    
    print(s2)
    ---------------------------------------------------------------------
    我叫Hassgy, 今年15岁了, 我喜欢Amy
    
    

    3.({0},{2},{1}......).format('值1','值2'......) 指定位置格式化

    s3 = "我叫{0}, 今年{2}岁了, 我喜欢{1}".format("Hassgy", "Amy", 15) 
    
    print(s3)
    ---------------------------------------------------------------------
    我叫Hassgy, 今年15岁了, 我喜欢Amy
    
    

    4.({变量名}......).format(变量名='值1',......) 关键字格式化

    s4 = "我叫{name}, 今年{age}岁了, 我喜欢{singer}".format(name="Hassgy", singer="Amy", age=15) 
    
    print(s4)
    ---------------------------------------------------------------------
    我叫Hassgy, 今年15岁了, 我喜欢Amy
    
    

    查询(查找)

    1.startswith('str'), endswith('str') 判断是否以什么开头/结尾的字符串

    >>> s1 = "我叫sylar, 我喜欢python, java, c等编程语⾔."
    
    >>> ret1 = s1.startswith("sylar")
    
    >>> print(ret1)
    
    False
    >>> ret1 = s1.endswith("语⾔.")
    >>> print(ret1)
    True
    
    

    2.count('str') 查找str出现的次数

    >>> s1 = "我叫sylar, 我喜欢python, java, c等编程语⾔."
    >>> ret2 = s1.count("a")
    
    >>> print(ret2)
    
    3
    
    

    3.find('str') 查找str出现得位置,如果找不到,返回-1

    >>> s1 = "我叫sylar, 我喜欢python, java, c等编程语⾔."
    >>> ret3 = s1.find("sylar")
    
    >>> print(ret3)
    
    2
    
    >>> ret3 = s1.find("tory")
    
    >>> print(ret3)
    
    -1
    
    

    4.find('str', n, m) 通过切片去找

    >>> s1 = "我叫sylar, 我喜欢python, java, c等编程语⾔."
    >>> ret4 = s1.find("a", 8, 22)
     >>> print(ret4)
     21
    
    

    5.index('str') 返回字符串的索引值

    >>> s1 = "我叫sylar, 我喜欢python, java, c等编程语⾔."
    >>> ret5 = s1.index("sylar")
    
    >>> print(ret5)
    
    2
    
    

    条件判断(字母数字)

    1.isalnum()

    s1 = "123.16"
    
    s2 = "abc"
    
    s3 = "_abc!@"
    
    print(s1.isalnum())
    
    print(s2.isalnum())
    
    print(s3.isalnum())
    
    

    2.isalpha()

    s1 = "123.16"
    s2 = "abc"
    s3 = "_abc!@"
    
    print(s1.isalpha())
    
    print(s2.isalpha())
    
    print(s3.isalpha())
    
    

    3.isdigit(),isdecimal(),isnumeric(),

    s1 = "123.16"
    s2 = "abc"
    s3 = "_abc!@"
    
    print(s1.isdigit())
    
    print(s1.isdecimal())
    
    print(s1.isnumeric()) # 这个⽜B点. 中⽂都识别.
    
    print(s2.isdigit())
    
    print(s3.isdigit())
    
    

    字符串长度

    len(str) 计算字符串长度

    s1 = 'hassgy'
    res = len(s1)
    print(res)
    ----------------------------
    6
    
    

    还有其他的方法,有兴趣的话可以上W3C或者菜鸟教程等网站进行查询其他内置函数以及转义字符的使用,后者比较关键。

    👉菜鸟教程:https://www.runoob.com/python3/python3-string.html

    相关文章

      网友评论

        本文标题:6.string类型(内置函数)

        本文链接:https://www.haomeiwen.com/subject/qudlrctx.html