2012-03-25 2 views
6

문자열은 모듈이고 str은 형식입니다. str에 메서드가 있고 str의 메서드 중 일부가 문자열과 동일한 것으로 나타났습니다.파이썬에서 문자열 메소드와 str 메소드의 차이점은 무엇입니까?

>>>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이나 문자열이 서로의 메서드를 사용할 수 있습니까?

답변

8

string은 더 이상 사용되지 않는 모듈입니다. 항상 str 개체를 사용해야합니다. 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. 
+0

지금 참조 : 예를 들어, 다음 문자열 모듈의 낮은 함수의 소스 코드입니다. 감사! 항상 – Huo

+2

? 'ascii_letters','maketrans' 및'string'의 유용한 메소드/속성은 어떻습니까? –

+0

그래, 항상 아니지만, 원칙적으로. – ronakg

6

기존 string 모듈 대부분은 STR 방법 주위 얇은 래퍼 함수로 재 기입한다.

# 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() 
+0

도움을 주셔서 감사합니다! – Huo

관련 문제