python字符串(2)

首先Python中的字符串对象依赖于str类,类里面包含了我们多使用到的所有方法,代码详见如下:

class str(basestring):
    """String object."""

    def __init__(self, object=''):
        """Construct an immutable string.#构造一个不可变的字符串,初始化对象使用

        :type object: object
        """
        pass

    def __add__(self, y):
        """The concatenation of x and y.#连接x和y  x,y均为字符串类型

        :type y: string
        :rtype: string
        """
        return b''

    def __mul__(self, n):
        """n shallow copies of x concatenated.#n浅层副本x连接。就是字符串的n倍出现连接
        :type n: numbers.Integral
        :rtype: str
        """
        return b''

    def __mod__(self, y): #求余
        """x % y.        

        :rtype: string
        """
        return b''

    def __rmul__(self, n):
        """n shallow copies of x concatenated.#n浅层副本x连接。就是字符串的n倍出现连接
:type n: numbers.Integral
        :rtype: str
        """
        return b''

    def __getitem__(self, y):
        """y-th item of x, origin 0.  #实现类似slice的切片功能

        :type y: numbers.Integral
        :rtype: str
        """
        return b''

    def __iter__(self):      #迭代器
        """Iterator over bytes.

        :rtype: collections.Iterator[str]
        """
        return []

    def capitalize(self):
        """Return a copy of the string with its first character capitalized  #实现首字母大写
        and the rest lowercased.

        :rtype: str
        """
        return b''

    def center(self, width, fillchar=' '):  #中间对齐
        """Return centered in a string of length width.

        :type width: numbers.Integral
        :type fillchar: str
        :rtype: str
        """
        return b''

    def count(self, sub, start=None, end=None): #计算字符在字符串中出现的次数
        """Return the number of non-overlapping occurrences of substring
        sub in the range [start, end].

        :type sub: string
        :type start: numbers.Integral | None
        :type end: numbers.Integral | None
        :rtype: int
        """
        return 0

    def decode(self, encoding='utf-8', errors='strict'): #把字符串转成Unicode对象
        """Return a string decoded from the given bytes.

        :type encoding: string
        :type errors: string
        :rtype: unicode
        """
        return ''

    def encode(self, encoding='utf-8', errors='strict'):#转换成指定编码的字符串对象
        """Return an encoded version of the string as a bytes object.

        :type encoding: string
        :type errors: string
        :rtype: str
        """
        return b''

    def endswith(self, suffix, start=None, end=None):#是否已xx结尾
        """Return True if the string ends with the specified suffix,
        otherwise return False.

        :type suffix: string | tuple
        :type start: numbers.Integral | None
        :type end: numbers.Integral | None
        :rtype: bool
        """
        return False

    def find(self, sub, start=None, end=None):#字符串的查找
        """Return the lowest index in the string where substring sub is
        found, such that sub is contained in the slice s[start:end].

        :type sub: string
        :type start: numbers.Integral | None
        :type end: numbers.Integral | none
        :rtype: int
        """
        return 0

    def format(self, *args, **kwargs):#格式化字符串
        """Perform a string formatting operation.

        :rtype: string
        """
        return ''

    def index(self, sub, start=None, end=None):#查找字符串里子字符第一次出现的位置
        """Like find(), but raise ValueError when the substring is not
        found.

        :type sub: string
        :type start: numbers.Integral | None
        :type end: numbers.Integral | none
        :rtype: int
        """
        return 0

    def isalnum(self):#是否全是字母和数字
        """Return true if all characters in the string are alphanumeric and
        there is at least one character, false otherwise.

        :rtype: bool
        """
        return False

    def isalpha(self):#是否全是字母
        """Return true if all characters in the string are alphabetic and there
        is at least one character, false otherwise.

        :rtype: bool
        """
        return False

    def isdigit(self):#是否全是数字
        """Return true if all characters in the string are digits and there
        is at least one character, false otherwise.

        :rtype: bool
        """
        return False

    def islower(self):#字符串中的字母是否全是小写
        """Return true if all cased characters in the string are lowercase
        and there is at least one cased character, false otherwise.

        :rtype: bool
        """
        return False

    def isspace(self):#是否全是空白字符
        """Return true if there are only whitespace characters in the
        string and there is at least one character, false otherwise.

        :rtype: bool
        """
        return False

    def istitle(self):#是否首字母大写
        """Return true if the string is a titlecased string and there is at
        least one character, for example uppercase characters may only
        follow uncased characters and lowercase characters only cased ones.

        :rtype: bool
        """
        return False

    def isupper(self):#字符串中的字母是都大写
        """Return true if all cased characters in the string are uppercase
        and there is at least one cased character, false otherwise.

        :rtype: bool
        """
        return False

    def join(self, iterable):#字符串的连接
        """Return a string which is the concatenation of the strings in the
        iterable.

        :type iterable: collections.Iterable[string]
        :rtype: string
        """
        return ''

    def ljust(self, width, fillchar=' '):#输出字符左对齐
        """Return the string left justified in a string of length width.
        Padding is done using the specified fillchar (default is a space).

        :type width: numbers.Integral
        :type fillchar: str
        :rtype: str
        """
        return b''

    def lower(self):#字符中的字母是否全是小写
        """Return a copy of the string with all the cased characters
        converted to lowercase.

        :rtype: str
        """
        return b''

    def lstrip(self, chars=None):#取出空格及特殊字符
        """Return a copy of the string with leading characters removed.

        :type chars: string | None
        :rtype: str
        """
        return b''

    def partition(self, sep):#字符串拆分 默认拆成三部分
        """Split the string at the first occurrence of sep, and return a
        3-tuple containing the part before the separator, the separator
        itself, and the part after the separator.

        :type sep: string
        :rtype: (str, str, str)
        """
        return b'', b'', b''

    def replace(self, old, new, count=-1):#字符串替换
        """Return a copy of the string with all occurrences of substring
        old replaced by new.

        :type old: string
        :type new: string
        :type count: numbers.Integral
        :rtype: string
        """
        return ''

    def rfind(self, sub, start=None, end=None):#右侧查找  第一次出现
        """Return the highest index in the string where substring sub is
        found, such that sub is contained within s[start:end].

        :type sub: string
        :type start: numbers.Integral | None
        :type end: numbers.Integral | none
        :rtype: int
        """
        return 0

    def rindex(self, sub, start=None, end=None):##右侧查找  第一次出现位置
"""Like rfind(), but raise ValueError when the substring is not
        found.

        :type sub: string
        :type start: numbers.Integral | None
        :type end: numbers.Integral | none
        :rtype: int
        """
        return 0

    def rjust(self, width, fillchar=' '):#右对齐
        """Return the string right justified in a string of length width.
        Padding is done using the specified fillchar (default is a space).

        :type width: numbers.Integral
        :type fillchar: string
        :rtype: string
        """
        return ''

    def rpartition(self, sep):#从右侧拆分
        """Split the string at the last occurrence of sep, and return a
        3-tuple containing the part before the separator, the separator
        itself, and the part after the separator.

        :type sep: string
        :rtype: (str, str, str)
        """
        return b'', b'', b''

    def rsplit(self, sep=None, maxsplit=-1):#字符串的分割
        """Return a list of the words in the string, using sep as the
        delimiter string.

        :type sep: string | None
        :type maxsplit: numbers.Integral
        :rtype: list[str]
        """
        return []

    def rstrip(self, chars=None):#去掉字符串的右侧空格
        """Return a copy of the string with trailing characters removed.

        :type chars: string | None
        :rtype: str
        """
        return b''

    def split(self, sep=None, maxsplit=-1):#字符串的切割
        """Return a list of the words in the string, using sep as the
        delimiter string.

        :type sep: string | None
        :type maxsplit: numbers.Integral
        :rtype: list[str]
        """
        return []

    def splitlines(self, keepends=False):#把字符串按照行切割成list
        """Return a list of the lines in the string, breaking at line
        boundaries.

        :type keepends: bool
        :rtype: list[str]
        """
        return []

    def startswith(self, prefix, start=None, end=None):#以xx开头
        """Return True if string starts with the prefix, otherwise return
        False.

        :type prefix: string | tuple
        :type start: numbers.Integral | None
        :type end: numbers.Integral | None
        :rtype: bool
        """
        return False

    def strip(self, chars=None):#去除左右空格
        """Return a copy of the string with the leading and trailing
        characters removed.

        :type chars: string | None
        :rtype: str
        """
        return b''

    def swapcase(self):#大小写互换
        """Return a copy of the string with uppercase characters converted
        to lowercase and vice versa.

        :rtype: str
        """
        return b''

    def title(self):#标题化字符串
        """Return a titlecased version of the string where words start with
        an uppercase character and the remaining characters are lowercase.

        :rtype: str
        """
        return b''

    def upper(self):#大写
        """Return a copy of the string with all the cased characters
        converted to uppercase.

        :rtype: str
        """
        return b''

    def zfill(self, width):#变成特定长度,不足0补齐
        """Return the numeric string left filled with zeros in a string of
        length width.

        :type width: numbers.Integral
        :rtype: str
        """
        return b''
以上是字符串类中的所有方法包含特殊方法。翻译不够准确,请谅解
时间: 2024-10-26 19:44:58

python字符串(2)的相关文章

Python字符串详细介绍

  这篇文章主要介绍了Python字符串详解,本文讲解了字符串相关知识.字符串的一些特性.原始字符串.unicode字符串.字符串的常用操作方法.内建函数列表等内容,需要的朋友可以参考下 简介 字符串序列用于表示和存储文本,python中字符串是不可变的,一旦声明,不能改变 通常由单引号(' ),双引号(" ),三引号(''' """)包围 其中三引号可以由多行组成,编写多行文本的快捷语法,常用语文档字符串,在文件的特定地点,被当做注释.便捷的多行注释 Python

Python字符串逐字符或逐词反转方法

  这篇文章主要介绍了Python字符串逐字符或逐词反转方法,本文对逐字符或逐词分别给出两种方法,需要的朋友可以参考下 目的 把字符串逐字符或逐词反转过来,这个蛮有意思的. 方法 先看逐字符反转吧,第一种设置切片的步长为-1 代码如下: revchars=astring[::-1] In [65]: x='abcd' In [66]: x[::-1] Out[66]: 'dcba' 第二种做法是采用reversed(),注意它返回的是一个迭代器,可以用于循环或传递给其它的"累加器",不

Python字符串格式化

  Python字符串格式化操作符(%)只适用于字符串类型,非常类似于C 语言里面的printf()函数的字符串格式化,甚至所用的符号都一样,都用百分号(%),并且支持所有printf()式的格式化操作. 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作%. 模板 格式化字符串时,Python使用一个字符串作为模板.模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格式.Python用一个tu

Python字符串处理之count()方法的使用

  这篇文章主要介绍了Python字符串处理之count()方法的使用,是Python入门的基础知识,需要的朋友可以参考下 count()方法返回出现在范围内串子数range [start, end].可选参数的start和end都解释为片符号. 语法 以下是count()方法的语法: ? 1 str.count(sub, start= 0,end=len(string)) 参数 sub -- 这是子串用来进行搜索. start -- 搜索从这一索引.第一个字符从0开始的索引.默认情况下搜索从0

Python字符串(Str)详解

字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可 字符串的格式 b = "hello itcast.cn" # 或者 b = 'hello itcast.cn' 双引号或者单引号中的数据,就是字符串 字符串连接的方法 直接通过加号(+)操作符连接 a = "str1" b = "str2" c = a + b print("a:%s" %

Python 字符串操作【转】

文章来源: http://www.cublog.cn/u/19742/showart_382176.html #Python字符串操作'''1.复制字符串''' #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2'print sStr2'''2.连接字符串''' #strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2print 

python字符串的处理问题

问题描述 python字符串的处理问题 给定一个字符串找出不同位置重复出现且长度最长的的第一个子字符串,输出子字符串以及其首字符的位置,若无任何重复子串,返回-1 解决方案 http://blog.csdn.net/littlethunder/article/details/25637173 解决方案二: ss = 'asd111111sda222wwwwwwqwqqaaassssdddd'index = len(ss) - 1ss_dict = {}ss_n = {}value_l = []d

python字符串,数值计算_python

Python是一种面向对象的语言,但它不像C++一样把标准类都封装到库中,而是进行了进一步的封装,语言本身就集成一些类和函数,比如print,list,dict etc. 给编程带来很大的便捷 Python 使用#进行单行注释,使用 ''' 或 """ 进行多行注释 数值计算 >>> print "One hour has", 60 * 60 , "seconds" One hour has 3600 seconds >>> re

浅谈Python 字符串格式化输出(format/printf)_python

Python 字符串格式化使用 "字符 %格式1 %格式2 字符"%(变量1,变量2),%格式表示接受变量的类型.简单的使用例子如下: # 例:字符串格式化 Name = '17jo'   print 'www.%s.com'%Name   >> www.17jo.com Name = '17jo' Zone = 'com' print 'www.%s.%s'%(Name,Zone) >> www.17jo.com 字符串格式化时百分号后面有不同的格式符号,代表

浅谈python字符串方法的简单使用_python

学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >>> s = 'wwwwww' >>> scap = s.capitalize() >>> scap 'Wwwwww' (2)s.center(width,char); 功能:返回将s字符串放在中间的一个长度为width的字符串,默认其他部分用空格填充,否则使用c