Category Archives: Python

How to Convert String into Integer in Python

Objects can be any kind of data type in Python, i.e. integer and string. Sometimes, you will need to convert one data form to another while writing Python code. For example, it needs to be transformed into an integer to perform a math operation on a number represented as a string. Here’s how to convert a string in Python to an integer.

int() function in Python

The built-in function int() returns an integer decimal object from a given number or String. It takes the form below:
int(a, base=10)

Two arguments are accepted by the function:

  • a – The number or String to be transformed to an integer.
  • base – Reflects the first argument’s numeral scheme. It can have a value between 0 and 2–36. It is optional to render this statement. The default base is always 10 if no base is defined (decimal integer). Integers are usually expressed in hexadecimal format i.e., base 16, decimal has base 10, octal has base 8, and binary has base 2.

If the given string cannot be interpreted as an integer, the ValueError exception is executed by the function.

Converting a String to Integer in Python

A ‘string’ is a list of characters declared in Python using a single (‘), double (“), or triple quotes (“””). If you use quotes to declare a variable that contains only numbers, its data type is set to String. Consider the instance below:

no_of_day = "23"
type(no_of_day)

The type() function tells us that the String object is the no_of_day variable.

<type 'str' > type

Let’s try on the variable to do a math operation:

print(day+5)

After that, Python throws an exception, i.e., TypeError, because it is unable to perform a string and integer addition calculation:

Traceback (last call last):
'' file, line 1, in
TypeError: can't concatenate items with 'str' and 'int.'

To convert a string representation of a decimal integer to an int, transfer a string that returns a decimal integer: to the int() function.

no_of_day = "23" days
no_of_day_int = int(no_of_day)
type(no_of_day_int)
<type 'int' >

The total operation will be done successfully if you try to do the math now:

print(no_of_day_int+5)
28

Remember, the commas you need to delete before passing the number to the int() function if the number contains commas, marking off thousands, millions, etc.:

total = "1,000,000"int(total.replace(",", ""))
1000000

When you execute the conversion between string to an integer, just make sure that you are using a correct base representing integers in various number systems. The number 54731 is expressed as D5CE in the hexadecimal method, for instance. You need to use Base 16 to convert it to a decimal integer:
If you move the D5CE String without setting the base to the int() function, the ValueError exception will be:

int ("D5CE", 16)
54731
Traceback (last call last):
'' file, line 1, in
ValueError: invalid literal with base 10 for int(): 'D5CF'

Conclusion:
In Python, using the int() function, you can convert a string to an integer.

How to Check Python Version

Python is one of the most successful programming languages in the world. It is used to develop websites, write scripts, machine learning, analyze data, and many more.

This article explains how to check what Python version is installed on your operating system working the command line. This can be beneficial when installing applications that require a particular version of python.

We will also explain to you how to programmatically determine what Python version is installed on the system where the Python script is running. For example, when writing Python scripts, you will require determining whether the script supports python’s version on the user’s machine.

Python Versioning

Python uses semantic versioning. Production-ready releases are versioned in the following scheme:

MAJOR.MINOR.MICRO

For example, in Python 3.6.8, 3 is a major version, 6 is a minor version, and 8 is a micro version.

  •  MAJOR – a python, has two major versions that are not fully cooperative: Python 2 and Python 3. For example, 3.5.7, 3.7.2, and 3.8.0 are all part of the Python 3 major version.
  • Minor: These announcements are bringing new features and roles. For example, 3.6.6, 3.6.7, and 3.6.8 are all part of the Python 3.6 minor version.
  • Micro: The new micro versions include various bug fixes and enhancements.

Development announcements have additional qualifiers. For more information, read the Python “Development Cycle” documentation.

Checking Python Version

Python is pre-installed on most maximum Linux distributions and macOS. On Windows, you have to download and install it if you want.

To get which version of python is fixed on your system, run the Python –version or Python -V command:

$ python --version

The order will print the default Python version, in this case, that is 2.5.15. The version installed on your system maybe another.

Output:

Python 2.7.15+

The default version of python will be applied by all scripts that have /usr/bin/python set as an interpreter in the script’s shebang line.

Some Linux distributions have various versions of python installed at the same time. The Python 3 binary is frequently named python3, and the Python 2 binary is called python or python2, but that may not always be the case.

You can check if you have Python 3 installed by typing:

$ python3 --version
Output:

Python 3.6.8

Python 2 support ended in 2020. Python 3 is the today and future of the programming language.

At the time of writing this article, the newest major release of python is version 3.8.x. The possibilities are that you can own an older version of Python 3 installed on your system.

If you want to install the most advanced python version, the method depends on your running system.

Programmatically Checking Python Version

Python 2 and Python 3 are different. The code written in Python 2.x may not work in Python 3.x, so you have to write two other codes.

The sys module that is open in all Python versions gives system-specific parameters and functions. sys.version_info enables you to discover the Python version installed on the system. It reflects a tuple that contains the five version numbers: major, minor, micro, release level, and serial.

Let’ say you have a script that needs at least Python version 3.5, and you want to tell whether the system meets requirements. You can do that by merely monitoring the major and minor versions:

import sys

if not (sys.version_info.major == 3 and sys.version_info.minor >= 5):

   print("This script requires Python 3.5 or higher!")

   print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))

   sys.exit(1)

If you run the script using Python version less than 3.5, it will produce the following output:

Output:

Python 3.5 or higher is required to run this script. 

You are using Python 2.5.

To write Python code that runs below both Python 3 and 2, use the future module. It enables you to run Python 3.x-compatible code below Python 2.

Conclusion: 

Using the python --version, it is very easy to find what version of python is installed on your system.