Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between string module and str

Tags:

What is the difference or relationship between str and string?

import string  print str print string  
like image 312
zjm1126 Avatar asked Jan 08 '10 07:01

zjm1126


People also ask

Is string and str same in Python?

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.

What is the purpose of STR string function?

The str() function converts the specified value into a string.

Is str a module in Python?

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.

Are strings in Python str objects?

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.


1 Answers

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.

like image 85
Stephan202 Avatar answered Feb 28 '23 13:02

Stephan202