Python has many inbuild function which is used to perform a specific operation. One such function is the str() function. In this blog, we will learn everything about the str python function. so let’s get started.
What is str function in python
In Python, the str
function is used to convert a given value into a string. The str function can be applied to a wide variety of data types, like integers, floats, etc.
Python str function syntax
Python str function follows the below syntax:
str(object, encoding=encoding, errors=errors)
where:
- object: the object to be converted to a string. This can be any data type such as an integer, a float, a list, etc.
- encoding: encoding is an optional parameter that specifies the encoding to be used when converting the object to a string. Encoding is used to represent the data in a specific format such as utf-8, ascii, etc. The default value is ‘utf-8’.
- errors: errors is also an optional parameter that specifies how to handle errors during the conversion. The default value is ‘strict’.Other options include ‘ignore’ and ‘replace’.
Str in python examples
In this session, we will understand the str function with various examples.
Python str function to convert integer
Python str function can convert an integer to a string value. let’s take the below example:
number = 45 number_str = str(number) print(number_str) print(type(number)) print(type(number_str))
- The first line creates an integer variable named
number
with a value of 45. - The second line converts the integer value of
number
to a string using thestr()
function, and assigns it to the variablenumber_str
. - The third line uses the
print()
function to output the value ofnumber_str
, which is “45”. - The fourth line uses the
print()
function with thetype()
function to output the data type ofnumber
, which is<class 'int'>
, indicating that it is an integer. - The fifth line uses the
print()
function with thetype()
function to output the data type ofnumber_str
, which is<class 'str'>
, indicating that it is a string.
Python str function to convert float
similar to integer a user can also use the str() function to convert the float data type.
number = 32.88 number_str = str(number) print(number_str) print(type(number)) print(type(number_str))
The above codes convert the float to string values.
Python str function with encoding
While converting any data type to string we can also pass the encoding and error parameters. Let’s take the below example:
data = b'Hello, World!' string_data = str(data, encoding='utf-8', errors='ignore') print(string_data) # "Hello, World!"
In this example, the str()
function is used to convert a bytes object data
which contains a byte sequence to a string, with the encoding
parameter set to ‘utf-8’ and the errors
parameter set to ‘ignore’. This will ignore any errors that occur during the conversion and return the string “Hello, World!”.
__str__ in python
When an object is converted to a string using the str() function, the str method is called to determine the string representation of the object.
str is a special method in Python that is used to define how an object should be represented as a string.
The str method takes no arguments and returns a string. we can customize this by defining our own str method. Let’s take the below example:
class TestClass: def __init__(self, country): self.country = country def __str__(self): return self.country obj = TestClass("UK") print(obj) print(str(obj))
- A class TestClass with init method which takes one argument ‘country’ and assigns it to an instance variable self.country.
- An str method, which returns the value of the self.country attribute of the object. So when the obj instance is printed or passed to the str() function, it will return the value of the self.country attribute which is “UK”
- An instance of the class TestClass named obj and passed “UK” as an argument for the country parameter, which printed the obj and passed obj to the str() function. Both return the value of the self.country attribute which is “UK”.
Conclusion
I hope you have liked this brief tutorial on the python str function. Please do let me know if you are facing any issues while following along.