Python string to int and Int to string: How to convert them in Python

We use Python to distinguish a particular type of data. For instance, Python strings can be used to represent text – based data. On the other hand, integers can represent the whole number. Therefore, sometimes you might want to convert values between different data types to work with them in different ways. Then, in this guideline, we will show you how to convert python string to int and Int to string. 

Different types of Python Data

python string to int

When working with Python, keep in mind that different types of data will be stored differently. For example, if you are working with text, the data will be stored as a string, while that for decimal numbers will be stored as float. However, in this guideline, we will mainly focus on “ string” and “ number”

Here is an example of String

  1. text_data = “This string contains text-based data.”

Strings include single or double quotes which will represent text – based information.

Moving to number data type, which can store both decimal as well as whole numbers. Decimal numbers are automatically converted into a float, while whole numbers will turn into integers. Here how an integer and a float – point number in Python looks like

  1. example_integer = 22
  2. example_float = 2.22

Python string to int

To convert a Python string to int, we will use the int() method. This method will return the value you put in int (), representing it as an integer.

Here the overall syntax of Python int() method

  1. int(number, base=base)

To illustrate, let’s consider this example. Here, we will convert a string into an integer

  1. print(int(“12”))

Output

  1. 12

Example of Python string to Int

Now, to understand the int() method more clearly, we deliver you this example. From this, you will be able to execute this method successfully

First, use the input () method to get user’s age

  1. raw_user_age = input(“What is your age?”)
  2. print(raw_user_age)

Once users enter a number, it will be printed out to the console. For instance, the user enter the number 12, here how the syntax looks like

  1. What is your age?
  2. 12
  3. 12

This looks like a number. However, when we use the type () method to check the data, it isn’t a number anymore. Use the following code to check the data of user’s age

  1. print(type(raw_user_age))

Output

  1. <class ‘str’>

The data’s class is “ str”, or “ string”. Then, in this case you need to convert Python string to int, or integer. To do that, use the following code

  1. raw_user_age = input(“What is your age?”)
  2. user_age = int(raw_user_age)
  3. print(user_age)

Then, when your user enters the value 12, it will be as below

  1. What is your age?
  2. 12
  3. 12

Now, you use the type () method to check the type of your data

  1. print(type(user_age))

Output

  1. <class ‘int’>

Python Int to String

Contrast to the above method, this time, we will show you to you can convert Python Int to String. The overall syntax of this method is

  1. str(number_to_convert)

Using the same example, we want to get the user’s age. And we converted the user’s age to an integer with this code

  1. raw_user_age = input(“What is your age?”)
  2. user_age = int(raw_user_age)

However, this time you want to print the message with the user’s age to the console.  Then, you use this code

  1. print(“Your age is: ” + user_age)

Since you can’t concatenate a string to an integer like above, this code cannot work. As a result, your code will return an error

  1. Traceback (most recent call last):
  2. File “main.py”, line 3, in <module>
  3. print(“Your age is: ” + user_age)
  4. TypeError: can only concatenate str (not “int”) to str

To make this code work, you should use the str() method

  1. raw_user_age = input(“What is your age?”)
  2. user_age = int(raw_user_age)
  3. as_string = str(user_age)
  4. print(“Your age is: ” + as_string)

Output will be

  1. What is your age?
  2. 12
  3. Your age is: 12

In conclusion

We hope that from our Python String to Int and Python Int to String tutorial, you will be able to execute it successfully. In case you meet any problems, or find it hard to do, then, don’t hesitate to contact ArrowHitech. Surely, you’ll receive our best service to cope with your problems

Moreover, If you want to open your own online store, and are looking for a trusty and enthusiastic partner, you’ll need to contact ArrowHitech. With more than 12 years in e-commerce, we can grant you an outstanding and professional-looking web store, regardless of the eCommerce platforms. Working with our decade-long experienced developers, you surely get your dream online shop. What are you waiting for? Contact us here or via email: support@arrowhitech.com or hotline + 84 243 7955 813

Tags

Share