Python str built-in function

The Python str built-in function returns a string of the object passed to it. If no argument/object is passed, it returns an empty string.

Consider the code below.

st='New string'
>>> str(st)
'New string'
>>> #Without any argument
>>> s=str()
>>> s
>>> #Passing list
>>> ls=['w' , 'we are' , 23 , (23)]
>>> str(ls)
"['w', 'we are', 23, 23]"
>>> #Passing dictionary
>>> str( {23:'New' , 89:"Eight" , 'Text':'Txt'} )
"{23: 'New', 89: 'Eight', 'Text': 'Txt'}"

Passing list and dictionary returns the string form of the list and the dictionary.

Number of arguments accepted by the str() function

The str() function can accept one or two or three arguments. Depending on the number of arguments passed the string returned by the function may vary. Each of the case when one or two or three arguments are passed is discussed below.

Passing one argument

If one argument is passed, the function returns the printable representation of the argument or object. An instance of this case is shown above. Here is another example.

Link : Python raw string literals

>>> str( r'raw string' ) #"r'string'"  is a raw string
'raw string'
>>> b=b'1234' #A byte
>>> str( b )

The section below explains how str() function works, so if you do not know what is class then do not read it, if you know what class is then you can continue.

Important point to note when passing any object to the str() function is if the object passed have the __str__() function then the str() calls this function and returns whatever is return by the __str__() function.

If the object do not have the __str__() function then the repr() function is called instead. The repr() function usually rerturns a string enclosed in angle brackets containing the name and the type of the object with additional information often including the name and address of the object.

Consider the code below. The class given below has the __str__() function

>>> #The 'New' class has __str__() function
>>> class New:
	def __init__(n , st , tp):
	def __str__(self):

>>> nm=New('Heap' ,678)
>>> str(nm)

Since __str__() returns the ‘st’ string so does the str(). Whatever __str__() return will be returned by the str().

Let’s look at another class where __str__() is not present.

>>> #The class "bike" has no __str__()
>>> class bike:
	def __init__(bk , st):

>>> b=bike('New true')
>>> str(bike)
'< object at 0x00343750>'

The returned value is a string enclosed in angle brackets and contain the type name and the address of the object.

By the way, We can also add the repr() function literally to the ‘bike’ class and controls what the str() returns. Do try it out.

Passing two or three arguments

When two or three arguments are passed the first argument is the object, the second argument is an encoding type and the third argument is the errors type.

If you mentioned the second or the third argument, the object passed must be of the ‘byte’ type. If the object does not belong to the byte type then the returned string is same as the string only when one argument is passed.

Consider the code below.

>>> #Passing one argument
>>> b=b'Byte string' #byte type
>>> str( b )
"b'Byte string'"
>>> #Mentioning the encoding type
>>> str( b , 'utf-8')
'Byte string'

Do you notice the difference? when the encoding type is mentioned the ‘b’ prefix is not included in the string returned by the str() function.

Leave a Reply

Your email address will not be published. Required fields are marked *