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'' 以上是字符串类中的所有方法包含特殊方法。翻译不够准确,请谅解
目录
相关文章
|
28天前
|
Python
在 Python 中,如何将日期时间类型转换为字符串?
在 Python 中,如何将日期时间类型转换为字符串?
121 64
|
4月前
|
存储 算法 数据库
使用python hashlib模块给明文字符串加密,以及如何撞库破解密码
`hashlib` 是 Python 中用于实现哈希功能的模块,它可以将任意长度的输入通过哈希算法转换为固定长度的输出,即散列值。该模块主要用于字符串加密,例如将用户名和密码转换为不可逆的散列值存储,从而提高安全性。`hashlib` 提供了多种哈希算法,如 `md5`、`sha1`、`sha256` 等。
72 1
|
20天前
|
存储 测试技术 Python
Python 中别再用 ‘+‘ 拼接字符串了!
通过选择合适的字符串拼接方法,可以显著提升 Python 代码的效率和可读性。在实际开发中,根据具体需求和场景选择最佳的方法,避免不必要的性能损失。
41 5
|
24天前
|
Python
使用Python计算字符串的SHA-256散列值
使用Python计算字符串的SHA-256散列值
25 7
|
1月前
|
Python
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
35 6
|
2月前
|
Python
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解
本篇将详细介绍Python中的字符串类型及其常见操作,包括字符串的定义、转义字符的使用、字符串的连接与格式化、字符串的重复和切片、不可变性、编码与解码以及常用内置方法等。通过本篇学习,用户将掌握字符串的操作技巧,并能灵活处理文本数据。
63 1
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解
|
3月前
|
Python
python获取字符串()里面的字符
在Python中,如果你想获取字符串中括号(比如圆括号`()`、方括号`[]`或花括号`{}`)内的字符,你可以使用正则表达式(通过`re`模块)或者手动编写代码来遍历字符串并检查字符。 这里,我将给出使用正则表达式的一个例子,因为它提供了一种灵活且强大的方式来匹配复杂的字符串模式。 ### 使用正则表达式 正则表达式允许你指定一个模式,Python的`re`模块可以搜索字符串以查找匹配该模式的所有实例。 #### 示例:获取圆括号`()`内的内容 ```python import re def get_content_in_parentheses(s): # 使用正则表达
120 36
|
2月前
|
自然语言处理 Java 数据处理
【速收藏】python字符串操作,你会几个?
【速收藏】python字符串操作,你会几个?
64 7
|
2月前
|
索引 Python
Python 高级编程:深入探索字符串切片
在Python中,字符串切片功能强大,可灵活提取特定部分。本文详细介绍切片技巧:基本切片、省略起始或结束索引、使用负数索引、设定步长及反转字符串等。此外,还介绍了如何结合其他操作进行切片处理,如先转换大小写再提取子串。 来源:https://www.wodianping.com/yeyou/2024-10/48238.html
47 4
|
3月前
|
Python
python第三方库-字符串编码工具 chardet 的使用(python3经典编程案例)
这篇文章介绍了如何使用Python的第三方库chardet来检测字符串的编码类型,包括ASCII、GBK、UTF-8和日文编码的检测示例。
166 6