Python - How To Convert String To Int

In this tutorial we will create on How To Convert String To Int Using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the coding...

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.
  1. from tkinter import *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
  1. #==================================MAIN FRAME=========================================
  2. root = Tk()
  3. root.title("Python - How To Convert String To Int")
  4. width=450
  5. height=250
  6. screen_width=root.winfo_screenwidth()
  7. screen_height=root.winfo_screenheight()
  8. x=(screen_width/2) - (width/2)
  9. y=(screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)

Assigning Variables

This is where the we will assign the variables. This code will assign each of the variables to be use later in the application.
  1. #=================================VARIABLES===========================================
  2. NUMBER = StringVar()

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
  1. #=================================FRAMES==============================================
  2. Top = Frame(root, relief=SOLID, bd=1)
  3. Top.pack(fill=X)
  4. Mid = Frame(root)
  5. Mid.pack(pady=20)
  6. Left = Frame(Mid)
  7. Left.pack(side=LEFT)
  8. Right = Frame(Mid)
  9. Right.pack(side=RIGHT)
  10. Bottom = Frame(root)
  11. Bottom.pack()
  12.  
  13. #=================================LABEL WIDGETS=======================================
  14. lbl_title = Label(Top, font=('calibri', 18), text="String To Int Data Type")
  15. lbl_title.pack()
  16. lbl_text = Label(Left, text="This is a string", font=('calibri', 14))
  17. lbl_text.pack()
  18. lbl_text2 = Label(Bottom, text="Result", font=('calibri', 14))
  19. lbl_text2.pack(side=TOP)
  20. lbl_result = Label(Bottom, font=('calibri', 12))
  21. lbl_result.pack()
  22.  
  23. #=================================ENTRY WIDGETS=======================================
  24. txt_str = Entry(Right, font=('calibri', 14), textvariable=NUMBER)
  25. txt_str.pack()
  26.  
  27. #=================================BUTTON WIDGETS======================================
  28. btn_convert = Button(Bottom, text="Convert", command=StringToInt)
  29. btn_convert.pack(pady=10)

Creating the Main Function

This is where the code that contains the main funcitions. This code will try to convert the string data into an integer data type. To do that just copy and write these blocks of code.
  1. #=================================METHODS=============================================
  2. def StringToInt():
  3.     if NUMBER.get() == "":
  4.         lbl_result.config(text="Please enter a string", fg="yellow")
  5.     else:
  6.         # Will convert the String Into Integer Data Type
  7.         if is_number(NUMBER.get()):
  8.             integer = int(NUMBER.get())
  9.             lbl_result.config(text="The value \"" + NUMBER.get() + "\" is a string, converted to " + str(integer) + " Integer data type", fg="Green")
  10.         else:
  11.             lbl_result.config(text="This is not a string number", fg="Red")
  12.  
  13. def is_number(s):
  14.     try:
  15.         float(s)
  16.         return True
  17.     except ValueError:
  18.         pass
  19.     try:
  20.         import unicodedata
  21.         unicodedata.numeric(s)
  22.         return True
  23.     except (TypeError, ValueError):
  24.          pass
  25.     return False

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. #=================================INITIALIZATION======================================
  2. if __name__ == '__main__':
  3.     root.mainloop()
There you have it we just created on How To Convert String To Int Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment