• Python数据类型---字符串


    字符串的操作方法:

    Python的数据类型字符串、布尔类型、列表、元组、字典等等,今天就简单介绍一下字符串的操作方法。

    Python的学习就是要多练习,好记性不如烂笔头,我们在实际中看看吧。

    下面就列举一些例子供大家参考:

    字符串的定义必须使用单引号或者双引号,这两个没有什么区别。

    name = 'winter'    #定义一个字符串
    print(type(name)) #查看南数据类型,type是内置的一个方法可以查看数据类型
    print(name) #打印出字符串
    运行结果:

    <class 'str'>   
    winter

    下面来查看一下字符串内置的一些方法(下面仅列举一些常用的方法)

    def __contains__(self, *args, **kwargs): # real signature unknown
    """ Return key in self. """
    查看字符是否在字符串中
    name = 'winter'    #定义一个字符串
    result = name.__contains__('w') #判断字符是否在字符串中,返回值为布尔值
    result1 = name.__contains__('s')
    print(result)
    print(result1)

    True       w在字符串winter中,因此返回True
    False       s不在字符串winter中,因此返回False

    contains和in一样都是判断字符是否在字符串中

    name = 'winter'
    result = 'w' in name
    print(result)

    True

    def __eq__(self, *args, **kwargs): # real signature unknown
    """ Return self==value. """
    判断字符串的只是否相等
    name = 'winter'
    result = name.__eq__('winter')
    result1 = name.__eq__('thunder')
    print(result)
    print(result1)

    True
    False

    def capitalize(self): # real signature unknown; restored from __doc__
    """
    B.capitalize() -> copy of B

    Return a copy of B with only its first character capitalized (ASCII)
    and the rest lower-cased.
    首字母变大写
    name = 'winter'
    result = name.capitalize()
    print(result)
    print(name)

    Winter      
    winter

    def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
    """
    B.center(width[, fillchar]) -> copy of B

    Return B centered in a string of length width. Padding is
    done using the specified fill character (default is a space).
    """ 返回B以一长串宽度为中心。 填充是使用指定的填充字符(默认为空格)
    name = 'winter'      #定义一个字符串
    result = name.center(20) #打印20个字符串,以winter为中心
    print(len(result)) #打印result的字符串长度
    print(result) 打印result的值,字符串长度不够,默认使用空格填充
    result1 = name.center(20,'*') #打印20个字符串,字符串长度不够,默认使用*填充
    result2 = name.center(20,'-') #打印20个字符串,字符串长度不够,默认使用-填充
    print(result1)
    print(result2)

    20
    winter
    *******winter*******
    -------winter-------

    def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """
    B.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of subsection sub in
    bytes B[start:end]. Optional arguments start and end are interpreted
    as in slice notation.
    """ 统计字符出现的次数,加参数可以统计某个范围内的次数
    name = 'welcometolearnpython'
    result = name.count('l') #统计字符l出现的次数
    result1 = name.count('l',0,9) #统计前八个字符中l出现的次数,包括前面的数字不包括后面的
    print(result)
    print(result1)

    2
    1

    def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
    """
    B.endswith(suffix[, start[, end]]) -> bool

    Return True if B ends with the specified suffix, False otherwise.
    With optional start, test B beginning at that position.
    With optional end, stop comparing B at that position.
    suffix can also be a tuple of bytes to try.
    """ 判断字符串是否以某个字符结尾
    name = 'winter'
    result = name.endswith('r') #判断字符串是否以r结尾
    result1 = name.endswith('s') #判断字符串是否以s结尾
    result2 = name.endswith('t',0,4) #判断前四个字符是否以t结尾
    print(result)
    print(result1)
    print(result2)

    True
    False
    True

    def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """
    B.find(sub[, start[, end]]) -> int

    Return the lowest index in B where subsection sub is found,
    such that sub is contained within B[start,end]. Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure. #判断字符出现在字符串中的位置
    name = 'winterorthunder'
    result = name.find('n') #判断字符n出现的位置,但是只标注第一次出现过的位置,当字符不存在的时候返回-1
    result1 = name.find('s')
    print(result)
    print(result1)
    print(name)

    2
    -1

    def __format__(self): # real signature unknown; restored from __doc__
    """
    complex.__format__() -> str

    Convert to a string according to format_spec.
    """ #转换字符串
    name = 'name:{0} 
    age:{1}'
    result = name.format('winter','25')
    name1 = 'Name:{name} Age:{age}'
    result1 = name1.format(name = 'winter',age = '25')
    print(result)
    print(result1)

    name:winter
    age:25
    Name:winter
    Age:25

    def partition(self, sep): # real signature unknown; restored from __doc__
    """
    S.partition(sep) -> (head, sep, tail)

    Search for the separator sep in S, and return the part before it,
    the separator itself, and the part after it. If the separator is not
    found, return S and two empty strings.
    """ #字符串分割
    name = 'winterlovethunder'
    result = name.partition('love') #以love把字符串分隔
    print(result)

    ('winter', 'love', 'thunder')

    def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
    """
    S.replace(old, new[, count]) -> str

    Return a copy of S with all occurrences of substring
    old replaced by new. If the optional argument count is
    given, only the first count occurrences are replaced.
    """ #字符串替换
    name = 'winterwinterwinter'
    result = name.replace('w','h') #把字符串中的w替换成h
    result2 = name.replace('w','h',2) #把字符串中的w替换成h,只替换两次
    print(result)
    print(result2)
    print(name)

    hinterhinterhinter
    hinterhinterwinter
    winterwinterwinter

    ljust方法是返回字符串左对齐的字符串宽度,填充是通过指定的fillchar(默认是空格),如果宽度小于字符串长度返回原始字符串

    ljust语法:str.ljust(width[, fillchar])

    参数:

    1,width---填充后字符串的总长度

    2,fillchar ---填充字符,默认是空格

    例子:

    name = 'winter'
    result = name.ljust(20)
    result1 = name.ljust(20,'!') #输出的总长度为20,不够的话使用!填充
    print(len(name)) #输出原来字符串的长度
    print(len(result)) #输出填充后的字符串长度
    print(result) #输出没有指定填充符的字符串,默认是空格
    print(result1) #输出没有指定填充符的字符串

    6
    20
    winter
    winter!!!!!!!!!!!!!!

    rjust方法是返回原字符串右对齐,并使用空格填充至长度的新字符串。填充是通过指定的fillchar(默认是空格),如果宽度小于字符串长度返回原始字符串

    rjust语法:str.rjust(width[, fillchar])

    参数:

    1,width---填充后字符串的总长度

    2,fillchar ---填充字符,默认是空格

    例子:

    name = 'winter'
    result = name.rjust(20)
    result1 = name.rjust(20,'!') #输出的总长度为20,不够的话使用!填充
    print(len(name)) #输出原来字符串的长度
    print(len(result)) #输出填充后的字符串长度
    print(result) #输出没有指定填充符的字符串,默认是空格
    print(result1) #输出没有指定填充符的字符串

    6
    20
    winter
    !!!!!!!!!!!!!!winter

    在此就不一一列举了,有问题的朋友可以联系我,页欢迎大家向我提出意见,谢谢大家。

    本人QQ:1127000483

     


  • 相关阅读:
    统计某个状态最新出现的连续次数
    debian 10 xface 安装输入法
    Temporary failure in name resolution
    Leetcode199二叉树的右视图(宽搜)
    Leetcode200岛屿数量(深搜)
    Leetcode130. 被围绕的区域(深搜)
    Leetcode116. 填充每个节点的下一个右侧节点指针(宽搜或深搜)
    Leetcode之二叉树展开为链表(深搜)
    Leetcode之路径总和II
    vue学习02-v-text
  • 原文地址:https://www.cnblogs.com/winter1519/p/6978899.html
Copyright © 2020-2023  润新知