python string 模块和 str 的关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2026038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Relationship between string module and str
提问by zjm1126
What is the difference or relationship between strand string?
str和string之间有什么区别或关系?
import string
print str
print string
回答by Stephan202
str
is a built-in function(actually a class) which converts its argument to a string. string
is a modulewhich provides common stringoperations.
str
是一个内置函数(实际上是一个类),它将其参数转换为字符串。string
是一个提供常见字符串操作的模块。
>>> str
<class 'str'>
>>> str(42)
'42'
>>> import string
>>> string
<module 'string' from '/usr/lib/python3.1/string.py'>
>>> string.digits
'0123456789'
Put another way, str
objects are a textual representation of some object o
, often created by calling str(o)
. These objects have certain methodsdefined on them. The module string
provides additional functionsand constantsthat are useful when working with strings.
换句话说,str
对象是某个对象的文本表示o
,通常通过调用str(o)
. 这些对象上定义了某些方法。该模块string
提供了在处理字符串时有用的附加函数和常量。
回答by Dave Kirby
There is some overlap between the string module and the str type, mainly for historical reasons. In early versions of Python str objects did not have methods, so all string manipulation was done with functions from the string module. When methods were added to the str type (in Python 1.5?) the functions were left in the string module for compatibility, but now just forward to the equivalent str method.
string 模块和 str 类型之间存在一些重叠,主要是历史原因。在 Python 的早期版本中, str 对象没有方法,因此所有字符串操作都是使用 string 模块中的函数完成的。当方法被添加到 str 类型时(在 Python 1.5 中?),为了兼容性,函数被保留在 string 模块中,但现在只是转发到等效的 str 方法。
However the string module also contains constants and functions that are not methods on str, such as formatting, character translation etc.
然而,string 模块还包含常量和函数,这些常量和函数不是 str 上的方法,例如格式化、字符转换等。
回答by aatifh
Like Stephan202 said: str is a built in function which is just used to convert item into string. It has also many useful methods. For instance:
就像 Stephan202 说的: str 是一个内置函数,只是用来将 item 转换为 string。它还有很多有用的方法。例如:
>>> str(100)
'100' # converts integer into string.
>>> str.lower('foobar')
'FOOBAR'
Now let's talk about String.-- It a python module which has very interesting functions. One of them Template thingy
现在让我们谈谈String。--它是一个python模块,具有非常有趣的功能。其中之一 模板东西
>>> from string import Template
>>> t = Template('$foo is a test')
>>> t.substitute (foo='this')
'this is a test' # Replaces $foo variable with 'this'
There are other useful methods. Suppose you want all the ascii letters
还有其他有用的方法。假设你想要所有的 ascii 字母
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
回答by user7360021
String is a module but has the same kind of ability as a class 'str' does.
String 是一个模块,但具有与类“str”相同的能力。
I suggest typing 'string' and type a '.' and wait for few seconds and a pop-down list will appear and you will see that many of the methods available there are also parts of 'str'.
我建议输入“字符串”并输入“.” 并等待几秒钟,将出现一个弹出列表,您将看到许多可用的方法也有“str”的一部分。
>>>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']
['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', '只是','lower', '小写', 'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip ', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']
or you can do this:
或者你可以这样做:
>>>help(string)
now you have to import String to use but str does not have that kind of limitations.
现在您必须导入 String 才能使用,但 str 没有这种限制。
>>>help(str)
This gives a similar looking list as well.
这也给出了一个类似的列表。
So the basic difference is that you have to import String but not str.
所以基本的区别是你必须导入 String 而不是 str。
回答by Matthew Iselin
"string" is a module that provides string handling functions, str is a built-in function that converts an object to a string representation. No relationship between the two.
"string" 是一个提供字符串处理函数的模块,str 是一个内置函数,可以将对象转换为字符串表示。两者之间没有任何关系。