Python错误,"'module'对象没有属性'lstrip'"

5 python string python-3.x

来自http://docs.python.org/library/string.html的 Python文档:

string.lstrip(s[, chars])

返回删除了前导字符的字符串副本.如果省略字符None删除空格字符.如果给定而不是None,则字符必须是字符串; 字符串中的字符将从调用此方法的字符串的开头删除."

Python 3.1.2 (r312:79360M, Mar 24 2010, 01:33:18) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> import string
>>> x = 'Hi'
>>> string.lstrip(x,1)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    string.lstrip(x,1)
AttributeError: 'module' object has no attribute 'lstrip'
>>> 
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

Sil*_*ost 7

py3k版本的文档位于:http://docs.python.org/py3k/index.html

string在py3k中删除了函数,你必须使用now str方法:

>>> x = 'Hi'
>>> x.lstrip('H')
'i'
Run Code Online (Sandbox Code Playgroud)

请注意,正如文档所述,chars 必须是一个字符串.不是整数.