This article explains how to convert a string into an integer in Python. When you are looking to program with Python, it’s essential that you are aware of the different data types that you may be dealing with. The way your data is stored will depend on which kind of data it is and each kind of data operates in a different manner.
Different data types in Python
Data types that you’re likely to encounter fall under the categories of numeric, string, and boolean. Simply put, numeric types deal with figures, including Integer (int) and Floating-Point numbers, Strings refer to characters, or sequences of characters, including letters and boolean refers to data that can be categorized as “true” or “false”. If you aren’t sure which data type you’re dealing with, you can use the type() function, as follows:
x = 1234 print (type (x))
This will bring up the response:
This response tells you that x is an integer. Integers can be as long as you like, or as long as your memory permits, but must be whole, decimal numbers. If, for example, you want to work with binary or hexadecimal integers, you will need to add in a string value to indicate that you are working with a base value other than 10, which is the default. The most important thing to note is that a number that includes a decimal point cannot be treated as an integer. Instead, such numbers are classified as floating-point types, which must be approached differently.
Strings (str), meanwhile, must be enclosed within delimiters, such as “here” or ‘look here’. Any characters within these delimiters are part of the string, and like integers, can be as long as you like. Strings can even be empty, for example: ” “.
How to turn a string into an integer
Bearing in mind that these data types must be treated differently, there are plenty of situations where you may want to change a string into an integer value. This is known as type conversion, and luckily, it can be achieved quite simply.
By using the int() method, you can take a string and have it returned as an integer, as follows:
int(number, base =base)
This syntax for the method shows that it takes two arguments, the number and the base value that you wish to be represented. This second parameter is optional, so your command can quite simply use the string on its own, for example:
This will result in 9 being displayed. Note that because the number 9 originates here as a string, it is defined within delimiters in the method.
Building on this, it’s then possible to apply type conversion to a range of examples. Perhaps you want to create a program that returns an integer value, for example, a lucky number, for a person playing a game. This will involve type conversion, and can be achieved as follows:
age = input("How old are you?")
The user can then enter their chosen number, which will be returned as a string. Although the string looks like a number as we read it, it is nonetheless of data type string, because it is taken in response to a string type prompt. This can be checked using the type() method. Therefore, the type conversion that we need has not yet been incorporated into our code. This can be achieved by altering the program slightly:
age = input("How old are you?") user_age = int(age) print(user_age)
If the user enters their age again, by following the prompt, and we then examine the data type, it will show as an integer value. By using the type() method again, we can see that the type conversion has indeed taken place, e.g.
will now return: