The str()
function in Python is a built-in function that converts the specified value into a string. It returns a string version of the object, which can be a number, a list, a tuple, or any other object, allowing it to be represented as a string.
Parameter Values
Parameter | Description |
---|---|
object | The object to be converted to a string. Can be an integer, float, list, tuple, dictionary, etc. |
encoding | Specifies the encoding of the string. |
errors | Specifies how encoding and decoding errors are to be handled. |
Return Values
The str()
function in Python returns a str
object representing the given object.
How to Use str()
in Python
Example 1:
The str()
function converts the specified value into a string.
x = 10
s = str(x)
print(s)
Example 2:
It can be used to convert different data types to strings.
num = 3.14
str_num = str(num)
print('The value of pi is: ' + str_num)
Example 3:
You can also convert boolean values to strings using str()
.
is_valid = True
str_valid = str(is_valid)
print('Is the input valid? ' + str_valid)