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'' 以上是字符串类中的所有方法包含特殊方法。翻译不够准确,请谅解
AI 代码解读
目录
打赏
0
0
0
0
1
分享
相关文章
Python正则表达式:用"模式密码"解锁复杂字符串
正则表达式是处理字符串的强大工具,本文以Python的`re`模块为核心,详细解析其原理与应用。从基础语法如字符类、量词到进阶技巧如贪婪匹配与预定义字符集,结合日志分析、数据清洗及网络爬虫等实战场景,展示正则表达式的强大功能。同时探讨性能优化策略(如预编译)和常见错误解决方案,帮助开发者高效掌握这一“瑞士军刀”。最后提醒,合理使用正则表达式,避免过度复杂化,追求简洁优雅的代码风格。
34 0
|
15天前
|
解决Python报错:DataFrame对象没有concat属性的多种方法(解决方案汇总)
总的来说,解决“DataFrame对象没有concat属性”的错误的关键是理解concat函数应该如何正确使用,以及Pandas库提供了哪些其他的数据连接方法。希望这些方法能帮助你解决问题。记住,编程就像是解谜游戏,每一个错误都是一个谜题,解决它们需要耐心和细心。
63 15
Python中的“空”:对象的判断与比较
在Python开发中,判断对象是否为“空”是常见操作,但其中暗藏诸多细节与误区。本文系统梳理了Python中“空”的判定逻辑,涵盖None类型、空容器、零值及自定义对象的“假值”状态,并对比不同判定方法的适用场景与性能。通过解析常见误区(如混用`==`和`is`、误判合法值等)及进阶技巧(类型安全检查、自定义对象逻辑、抽象基类兼容性等),帮助开发者准确区分各类“空”值,避免逻辑错误,同时优化代码性能与健壮性。掌握这些内容,能让开发者更深刻理解Python的对象模型与业务语义交集,从而选择最适合的判定策略。
30 5
[oeasy]python083_类_对象_成员方法_method_函数_function_isinstance
本文介绍了Python中类、对象、成员方法及函数的概念。通过超市商品分类的例子,形象地解释了“类型”的概念,如整型(int)和字符串(str)是两种不同的数据类型。整型对象支持数字求和,字符串对象支持拼接。使用`isinstance`函数可以判断对象是否属于特定类型,例如判断变量是否为整型。此外,还探讨了面向对象编程(OOP)与面向过程编程的区别,并简要介绍了`type`和`help`函数的用法。最后总结指出,不同类型的对象有不同的运算和方法,如字符串有`find`和`index`方法,而整型没有。更多内容可参考文末提供的蓝桥、GitHub和Gitee链接。
46 11
Python中r前缀:原始字符串的魔法解析
本文深入解析Python中字符串的r前缀(原始字符串)的设计原理与应用场景。首先分析传统字符串转义机制的局限性,如“反斜杠地狱”问题;接着阐述原始字符串的工作机制,包括语法定义、与三引号结合的用法及特殊场景处理。文章重点探讨其在正则表达式、文件路径和多语言文本处理中的核心应用,并分享动态构建、混合模式编程等进阶技巧。同时纠正常见误区,展望未来改进方向,帮助开发者更好地理解和使用这一特性,提升代码可读性和维护性。
14 0
python字符串类型及操作
本文主要讲解字符串类型的表示、操作符、处理函数、处理方法及格式化。内容涵盖字符串的定义、表示方法(单双引号、三引号)、索引与切片、特殊字符转义、常见操作符(如+、*、in等)、处理函数(如len()、str()、chr()等)、处理方法(如.lower()、.split()等)以及格式化方式(如.format())。通过实例代码详细介绍了字符串的各种用法和技巧,帮助读者全面掌握字符串操作。
python字符串类型及操作
|
6月前
|
在 Python 中,如何将日期时间类型转换为字符串?
在 Python 中,如何将日期时间类型转换为字符串?
268 64
|
6月前
|
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
196 62
[oeasy]python061_如何接收输入_input函数_字符串_str_容器_ 输入输出
本文介绍了Python中如何使用`input()`函数接收用户输入。`input()`函数可以从标准输入流获取字符串,并将其赋值给变量。通过键盘输入的值可以实时赋予变量,实现动态输入。为了更好地理解其用法,文中通过实例演示了如何接收用户输入并存储在变量中,还介绍了`input()`函数的参数`prompt`,用于提供输入提示信息。最后总结了`input()`函数的核心功能及其应用场景。更多内容可参考蓝桥、GitHub和Gitee上的相关教程。
51 0
Python如何显示对象的某个属性的所有值
本文介绍了如何在Python中使用`getattr`和`hasattr`函数来访问和检查对象的属性。通过这些工具,可以轻松遍历对象列表并提取特定属性的所有值,适用于数据处理和分析任务。示例包括获取对象列表中所有书籍的作者和检查动物对象的名称属性。
64 2

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等