【问题标题】:what's the difference between string method and str method in Python?Python中的字符串方法和str方法有什么区别?
【发布时间】:2012-04-09 03:45:05
【问题描述】:

String 是一个模块,str 是一个类型。 我发现str有方法,str的一些方法和string是一样的。

>>>dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', 
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 
'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs',
'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace',
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace',
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']

>>> dir(string)
['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__',
'__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', 
'_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 
'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize',   
'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 
'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase',
'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind',
'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 
'translate', 'upper', 'uppercase', 'whitespace', 'zfill']

这两组方法有什么区别? str 和 string 可以互相使用对方的方法吗?

【问题讨论】:

    标签: python string methods


    【解决方案1】:

    string 现在是一个弃用的模块。您应该始终使用str 对象。

    >>> help(str)
    Help on class str in module __builtin__:
    
    class str(basestring)
     |  str(object) -> string
     |  
     |  Return a nice string representation of the object.
     |  If the argument is a string, the return value is the same object.
    .
    .
    >>>help(string)
    Help on module string:
    
    NAME
        string - A collection of string operations (most are no longer used).
    
    FILE
        /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/string.py
    
    MODULE DOCS
        http://docs.python.org/library/string
    
    DESCRIPTION
        Warning: most of the code you see here isn't normally used nowadays.
        Beginning with Python 1.6, many of these functions are implemented as
        methods on the standard string object. They used to be implemented by
        a built-in module called strop, but strop is now obsolete itself.
    

    【讨论】:

    • 总是? ascii_lettersmaketrans 以及来自string 的所有其他有用方法/属性怎么样?
    • 是的,可能并不总是,但作为一般规则。
    【解决方案2】:

    str 方法被添加到 Python 中时,许多现有的 string 模块 被重写为 str 方法周围的瘦包装函数。例如,这里是字符串模块中lower函数的源码:

    # convert UPPER CASE letters to lower case
    def lower(s):
        """lower(s) -> string
    
        Return a copy of the string s converted to lowercase.
    
        """
        return s.lower()
    

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2011-02-12
      • 1970-01-01
      相关资源
      最近更新 更多