开发者

Relationship between string module and str

开发者 https://www.devze.com 2022-12-15 23:28 出处:网络
What is t开发者_开发技巧he difference or relationship between str and string? import string print str

What is t开发者_开发技巧he difference or relationship between str and string?

import string 
print str
print string 


str is a built-in function (actually a class) which converts its argument to a string. string is a module which provides common string operations.

>>> 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 methods defined on them. The module string provides additional functions and constants that are useful when working with strings.


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.

However the string module also contains constants and functions that are not methods on str, such as formatting, character translation etc.


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:

>>> 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

>>> 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

>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'


String is a module but has the same kind of ability as a class 'str' does.

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'.

>>>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']

or you can do this:

>>>help(string)

now you have to import String to use but str does not have that kind of limitations.

>>>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" 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消