Today our builtin function is str() which is useful for converting any datatype in to a string. This is very much useful to convert one datatype to a string.

Need of str() function in Python:

Some times in programming when we try to club two datatypes it may give you TypeError like "TypeError: cannot concatenate ‘str’ and ‘int’ objects". In that case we can convert those data types in to strings and do manuplation and convert them back to desired data types. In this post we will see on how to use str() python built-in function.

str() function syntax:

str(‘number’)
 
Examples: Let us start with str() function with some examples and see how we can use them in Python coding.
 
Example1: Convert a number into a string .
 
>>> str(‘23’)
 
Output:
 
'23'
>>>
 
Example2: Adding a string which is number to other number

My variables are

STR2=23

NUM2='87'

Adding two numbers in which one is in string formats.

NUM3=str(STR2)+NUM2

Output:

2387

 

If you observe it concatenated two numbers instead of adding them. 
Related functions: chr(), str(), ord(), int()