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
分享
相关文章
|
13天前
|
Python f-strings:让字符串格式化更简洁高效!
Python f-strings:让字符串格式化更简洁高效!
135 81
|
13天前
|
Python字符串格式化利器:f-strings入门指南
Python字符串格式化利器:f-strings入门指南
120 80
|
13天前
|
Python高效字符串格式化:f-strings的魅力
Python高效字符串格式化:f-strings的魅力
104 80
Python中r前缀:原始字符串的魔法解析
本文深入解析Python中字符串的r前缀(原始字符串)的设计原理与应用场景。首先分析传统字符串转义机制的局限性,如“反斜杠地狱”问题;接着阐述原始字符串的工作机制,包括语法定义、与三引号结合的用法及特殊场景处理。文章重点探讨其在正则表达式、文件路径和多语言文本处理中的核心应用,并分享动态构建、混合模式编程等进阶技巧。同时纠正常见误区,展望未来改进方向,帮助开发者更好地理解和使用这一特性,提升代码可读性和维护性。
95 0
Python正则表达式:用"模式密码"解锁复杂字符串
正则表达式是处理字符串的强大工具,本文以Python的`re`模块为核心,详细解析其原理与应用。从基础语法如字符类、量词到进阶技巧如贪婪匹配与预定义字符集,结合日志分析、数据清洗及网络爬虫等实战场景,展示正则表达式的强大功能。同时探讨性能优化策略(如预编译)和常见错误解决方案,帮助开发者高效掌握这一“瑞士军刀”。最后提醒,合理使用正则表达式,避免过度复杂化,追求简洁优雅的代码风格。
89 0
|
25天前
|
Python编程基石:整型、浮点、字符串与布尔值完全解读
本文介绍了Python中的四种基本数据类型:整型(int)、浮点型(float)、字符串(str)和布尔型(bool)。整型表示无大小限制的整数,支持各类运算;浮点型遵循IEEE 754标准,需注意精度问题;字符串是不可变序列,支持多种操作与方法;布尔型仅有True和False两个值,可与其他类型转换。掌握这些类型及其转换规则是Python编程的基础。
140 33
|
2月前
|
解读 Python 3.14:模板字符串、惰性类型、Zstd压缩等7大核心功能升级
Python 3.14 引入了七大核心技术特性,大幅提升开发效率与应用安全性。其中包括:t-strings(PEP 750)提供更安全灵活的字符串处理;类型注解惰性求值(PEP 649)优化启动性能;外部调试器API标准化(PEP 768)增强调试体验;原生支持Zstandard压缩算法(PEP 784)提高效率;REPL交互环境升级更友好;UUID模块扩展支持新标准并优化性能;finally块语义强化(PEP 765)确保资源清理可靠性。这些改进使Python在后端开发、数据科学等领域更具竞争力。
99 5
解读 Python 3.14:模板字符串、惰性类型、Zstd压缩等7大核心功能升级
Python语言中字符串操作方法的全面归纳
以上就是Python中一些重要的字符串操作方法,掌握了这些,对于操作字符串,你也就够用了。在Python众多的特性中,字符串操作无疑是最有趣的部分之一。希望你也觉得如此。
67 27
|
3月前
|
解决Python报错:DataFrame对象没有concat属性的多种方法(解决方案汇总)
总的来说,解决“DataFrame对象没有concat属性”的错误的关键是理解concat函数应该如何正确使用,以及Pandas库提供了哪些其他的数据连接方法。希望这些方法能帮助你解决问题。记住,编程就像是解谜游戏,每一个错误都是一个谜题,解决它们需要耐心和细心。
164 15
Python中的“空”:对象的判断与比较
在Python开发中,判断对象是否为“空”是常见操作,但其中暗藏诸多细节与误区。本文系统梳理了Python中“空”的判定逻辑,涵盖None类型、空容器、零值及自定义对象的“假值”状态,并对比不同判定方法的适用场景与性能。通过解析常见误区(如混用`==`和`is`、误判合法值等)及进阶技巧(类型安全检查、自定义对象逻辑、抽象基类兼容性等),帮助开发者准确区分各类“空”值,避免逻辑错误,同时优化代码性能与健壮性。掌握这些内容,能让开发者更深刻理解Python的对象模型与业务语义交集,从而选择最适合的判定策略。
89 5

推荐镜像

更多
AI助理

你好,我是AI助理

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