E-Learning

Learn Python – How To Convert Integer to String?

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.

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:

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.

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:

 Aaryn

 Vivaed

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

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

3 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

1 month ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago