Learn Python – How To Convert Integer to String?

 
Earlier in  this series, 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.

ENJOYING THIS POST?

Get More Benefits With LiveEdu Pro

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.
 

Main differences between str () and repr () in Python

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:


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.
 

Why use the “informal” representation str () in Python?

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:

python python programming python string Aaryn

 

 

python python programming python string Vivaed

 

 

Another cool way to find out interesting things about Python is to access our project page!

  • Kevin Rodger

    Hey, Is there any simpler way to do it ?

Read previous post:
Learn JavaScript – How To Make Page Redirect Using jQuery
Learn JavaScript – How To Make Page Redirect Using jQuery

In programming languages, functions are often gathered together in packages and methods ready for use. They are called frameworks and aim to help...

Close