Convert string to integer in Python

Convert string to integer in Python

In Python an strings can be converted into a integer using the built-in `int()` function. The int() function takes in any python data type and converts it into a integer.But use of the `int()` function is not the only way to do so. This type of conversion can also be done using the`float()` keyword, as a float value can be used to compute with integers.

Below is the list of possible ways to convert an integer to string in python:

1. Using int() function

Syntax: int(string)

Example:

 `num ``=` `'10'` ` ` `# check and print type num variable` `print``(``type``(num)) ` ` ` `# convert the num into string ` `converted_num ``=` `int``(num)` ` ` `# print type of converted_num` `print``(``type``(converted_num))` ` ` `# We can check by doing some mathematical operations` `print``(converted_num ``+` `20``)`

 `num ``=` `'10.5'` ` ` `# check and print type num variable` `print``(``type``(num)) ` ` ` `# convert the num into string ` `converted_num ``=` `float``(num)` ` ` `# print type of converted_num` `print``(``type``(converted_num))` ` ` `# We can check by doing some mathematical operations` `print``(converted_num ``+` `20.5``)`

2. Using float() function

We first convert to float, then convert float to integer. Obviously the above method is better (directly convert to integer)

Syntax: float(string)

Example:

 `a ``=` `'2'` `b ``=` `'3'` ` ` `# print the data type of a and b` `print``(``type``(a))` `print``(``type``(b))` ` ` `# convert a using float` `a ``=` `float``(a)` ` ` `# convert b using int` `b ``=` `int``(b)` ` ` `# sum both integers` `sum` `=` `a ``+` `b` ` ` `# as strings and integers can't be added` `# try testing the sum` `print``(``sum``)`

Output:

```class 'str'
class 'str'
5.0```

Note: float values are decimal values that can be used with integers for computation.

Last Updated on October 24, 2021 by admin

Hash Map in PythonHash Map in Python

Hash Map in Python Hash maps are indexed data structures. A hash map makes use of a hash function to compute an index with a key into an array of buckets or slots. Its value is mapped to the bucket with the

Python – Get dictionary keys as a listPython – Get dictionary keys as a list

Python | Get dictionary keys as a list   Given a dictionary, write a Python program to get the dictionary keys as a list. Examples: Input : {1:'a', 2:'b', 3:'c'} Output : [1, 2, 3] Input : {'A' : 'ant',

Python – Remove square brackets from listPython – Remove square brackets from list

Python | Remove square brackets from list Sometimes, while working with displaying the contents of list, the square brackets, both opening and closing are undesired. For this when we need to print whole list without accessing the elements for loops,

Priority Queue in PythonPriority Queue in Python

Priority Queue in Python Priority Queues are abstract data structures where each data/value in the queue has a certain priority. For example, In airlines, baggage with the title “Business” or “First-class” arrives earlier than the rest. Priority Queue is an

Image Resizing using OpenCV | PythonImage Resizing using OpenCV | Python

Image Resizing using OpenCV | Python Image resizing refers to the scaling of images. Scaling comes in handy in many image processing as well as machine learning applications. It helps in reducing the number of pixels from an image and

Statement, Indentation and Comment in PythonStatement, Indentation and Comment in Python

Statement, Indentation and Comment in Python Instructions written in the source code for execution are called statements. There are different types of statements in the Python programming language like Assignment statements, Conditional statements, Looping statements, etc. These all help the

Python – Convert String to TuplePython – Convert String to Tuple

Python | Convert String to Tuple Interconversion of data types is very common problem one can face while programming. There can be a problem in which we need to convert a string of integers to a tuple. Let’s discuss certain