What is the difference or relationship between str and string?
import string print str print string
Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use). String literals can be enclosed by either double or single quotes, although single quotes are more commonly used.
The str() function converts the specified value into a string.
In Python 1.5. 2 and earlier, the string module uses functions from the strop implementation module where possible. In Python 1.6 and later, most string operations are made available as string methods as well, as shown in Example 1-52.
Strings are Objects. Strings are objects in Python which means that there is a set of built-in functions that you can use to manipulate strings. You use dot-notation to invoke the functions on a string object such as sentence.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With