String to int in Python

String to int in Python

In this tutorial, we will learn the ways of converting a string to integer in Python-

Let us have a look at an example before proceeding-

Output:

<class 'str'>
<class 'int'>

In the above example, we have declared the variables ‘a’ and ‘b’ with a string and an integer value respectively.

We can verify their data types using type().

The question that arises here is why do we need to convert a string to an integer.

The following program illustrates the same-

Output:

res = value_a * value_b

TypeError: can't multiply sequence by non-int of type 'str'

Since it generates this type of error, this is the reason that we must convert the string values to integers so that we can easily proceed with the operations.

It’s time to have a look at the first program that demonstrates converting a string to an integer.

Output:

<class 'str'>
<class 'int'>
17
<class 'int'>

Explanation:

Let’s see the explanation of the above program-

  1. The first step is to declare the variable ‘a’ with a string value.
  2. After this, we have checked its data type using type().
  3. For converting the string to an integer, we have used int() and then checked its type.
  4. Now we have operated on the variable ‘a’ by adding 10 to it.
  5. Finally, the resultant value is displayed in the output.

Approach-2

In the next example, we will go for an indirect approach of converting a string to an integer.

The following program shows how it can be done-

Output:

<class 'str'>
<class 'str'>
The sum of value_a and value_b is  126.0

Explanation:

Let us understand what we have done in the above program-

  1. The first step is to declare the two variables ‘value_a’ and ‘value_b’ with a string value.
  2. After this, we have checked their data type using type().
  3. For converting the string to an integer, we have used float() to convert the string to float value.
  4. In the next step, we will convert the string value of ‘value_b’ to an integer.
  5. Now we have added ‘value_a’ and ‘value_b’ and printed their sum.
  6. Finally, the resultant value is displayed in the output.

Approach-3:

In the last program, we will discuss one more scenario of converting string to int in Python.

Here we will see how we can convert a number present as a string value to base 10 when it is on different bases.

The following program illustrates the same-

Output:

The value of num_value is: 234
The value of num_value from base 10 to base 10 is: 234
The value of num_value from base 8 to base 10 is: 156
The value of num_value base 6 to base 10 is: 94

Explanation:

It’s time to have a glance at the explanation of the above program.

  1. In the first step, we have declared the value of the variable.
  2. Since the output will always be in base 10, we have provided the different base values inside int().
  3. The base values we have taken here are 10, 8, and 6.
  4. On executing the program, the expected output is displayed.

Conclusion

In this tutorial, we learned the different ways of converting a string to an int value.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263396.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论