TechJunkie is a BOX20 Media Company

Home Web How To Convert an Integer to a String in Python

How To Convert an Integer to a String in Python

How To Convert an Integer to a String in Python

It’s reader’s question time again. This time I’m in unfamiliar territory, programming. Fortunately, I know a man who can and he was kind enough to help me with the answer. The question was ‘how do you convert an integer to a string in Python?’ As always, I endeavor to answer.

Python is over 25 years old and still going strong. It is a high level programming language used to create a variety of programs. It is a ‘general purpose’ language which can be turned into games, web apps, AI routines and even desktop programs. As it uses more plain language than code, it is very user friendly and even I managed to begin getting to grips with it. If I can, you certainly can!

First I’ll answer the reader’s question and then I’ll discuss Python a little more because I find myself very intrigued by it.

Convert integer to a string in Python

An integer is a number. Converting a number into a string is used for tables and ordered lists which play a part in many productivity apps written in Python. There are apparently a few ways to convert integer to a string in Python but I was shown the ‘str’ function so will use that in the example. Other ways include ‘repr’ and ‘%d’ %’.

The format is ‘str(int)’.

For example, if you typed the following into a Python editor, you could perform some basic math:

d = 100.0/52.0

s = str(d)

print s

The math is 100 divided by 52. The ‘str’ is added with (d) to indicate where to take the input from, then perform the match and print the result. In this case, the result is 1.9230769.

A slightly more complex version:

d = 100.0/52.0

#Use 8 characters, give 2 decimal places

s = “%8.2f” % d

print s

This code performs the same math but specifies only two decimal places in the result. So the print would then be 1.92.

So that is how to convert integer to a string in Python.

A bit of background on Python

As a writer for people, I have difficulty reading or even comprehending writing for machines. Sentences don’t make sense, the syntax is impossible to follow and I just don’t get it. However, Python is a little different. It seems more plain English than other programming languages.

That makes it very beginner friendly. You can quickly do something amusing or useful with the language which delivers that instant gratification we all crave. You can also do some cool stuff. That is why Python is now more popular than Java as the gateway programming language of choice for many colleges and universities across the world.

Once you realize the extent to which Python is used, it becomes much more interesting. For example, Industrial Light & Magic uses Python and so does Rackspace, Honeywell, Philips, AstraZenica, ForecastWatch, D-link and a range of other large companies. They all utilize Python for something within their business from handling processes to data management.

If you have ever used the Minecraft addon MCDungeon, it also uses Python to create the additional content. It also allows you to create your own if you have a little programming knowledge.

Using Python

Python can be downloaded for free here and is regularly developed and updated. The code is open source and is maintained by a huge number of developers. There is a Windows and Unix version available for free download to do with what you will.

There are currently two versions, Python 2 and Python 3. Version 2 is the legacy version of the language while Python 3 is the current and future version. If you are planning to learn Python for a specific program or app, you may need to download version 2. If you want to learn in order to create your own, version 3 will be fine. You can download both and run either should you wish to.

Once installed, you use Python from the command line. Open a command line window as an administrator, type ‘python3 –version’ and hit Enter. If you’re using version 2, you would obviously type ‘python2 –version’ and hit Enter. You will then be entered into the programming environment.

That is about as far as I have gotten with Python so far. This guide over at ‘The Hitchhiker’s Guide to Python’ can take you further should you wish to go.

I am more grammatically inclined that programmatically so Python is going to be difficult for me. Nevertheless, it has piqued my interest which is why I bored you with more stuff about the language that you probably didn’t want to know. At least I learned enough to be able to answer the original question!

How To Remove Favorites From Safari on Apple iPhone X

Read Next 

Leave a Reply

Your email address will not be published. Required fields are marked *


Random

Nov 29, 2017

944 Articles Published

More