Earlier, we saw how to parse a String to an Int or float in Python. Now, we want the opposite. How to do it?
In Python, there are two ways to transform any value into a string: the functions repr () and str ().
The str () function is used to create value representations that people can understand. The repr () function is used to create values that the Python interpreter can use.
For most values, such as integers, lists, and dictionaries, they have the same representation when using any of the functions. Floats and Strings, on another hand, are represented in different ways when using one or other function.
The only difference between the two functions is the way they present the values? As a matter of fact, no. Let’s look this subject a little deeper.
At the Python official documentation, we read that repr () and str () are used to “represent” an object. The str () is considered an “informal” representation. The repr () function would be the “formal” representation.
Below we have some examples where can be seen some of these differences:
s = 'Hello, world.' str(s) Hello world program repr(s) Hello world program str(1.0/7.0) '0.142857142857' repr(1.0/7.0) '0.14285714285714285'
This difference between the returned values in the above examples can be verified within the system. The default return value of repr () function to an object can be called as an argument within the eval () function. The return of eval() will be a valid object. We can see this in the example below:
1 | >>> Y = ‘string’ |
2
3 4 5 6 |
>>> Repr (y)
” ‘String'” >>> Y 2 = eval (repr (y)) >>> Y == y2 True |
The return of the str () function is shown as not valid. Example below:
1 | >>> Y = ‘string’ |
2
3 4 5 6 7 8 9 10 |
>>> Str (y)
‘String’ >>> Eval (str (y)) Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “<string>”, line 1 the string ^ SyntaxError: unexpected EOF while parsing |
Thus, the “formal” representation of the object can be called by the eval () function, and must return the same object, if applicable.
It returns a briefer response of the value of the object. Complex objects, with several arguments, repr () function returns a value difficult to be analyzed. If we are only looking for a “quick view”, the best thing to use is the str () function.
Use the comments area below to share other tips or questions on this subject! If you want to browse more on other issues, you can check out our gallery videos about Python. Below are some examples:
You can also subscribe to some channels that are broadcast in Python, such as the following:
Another cool way to find out interesting things about Python is to access our project page!
In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…
We’re thrilled to announce an exciting opportunity for you to win not one but two…
Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…
Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…
Another week to bring you the top yield platforms for three of the most prominent…
If you hold a large volume of LEDU tokens above 1 million units and wish…
View Comments
Hey, Is there any simpler way to do it ?