Skip to main content

Python - Convert String To Integer Type


In this tutorial we will create a Convert String To Integer Type using Python. This code will convert the given string into integer data type when user click the convert button. The code use tkinter module to create a layout and widgets that can call python functions. When a function is called it the application will automatically convert the given string data type into integer data type using convert().

We will be using Python because it has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing..

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

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 - Convert String To Integer Type")
  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)

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. #=================================VARIABLES===========================================
  2. NUMBER = StringVar()
  4. #=================================FRAMES==============================================
  5. Top = Frame(root, relief=SOLID, bd=1)
  6. Top.pack(fill=X)
  7. Mid = Frame(root)
  8. Mid.pack(pady=20)
  9. Left = Frame(Mid)
  10. Left.pack(side=LEFT)
  11. Right = Frame(Mid)
  12. Right.pack(side=RIGHT)
  13. Bottom = Frame(root)
  14. Bottom.pack()
  16. #=================================LABEL WIDGETS=======================================
  17. lbl_title = Label(Top, font=('calibri', 18), text="Python - Convert String To Integer Type")
  18. lbl_title.pack()
  19. lbl_text = Label(Left, text="Enter a string number", font=('calibri', 14))
  20. lbl_text.pack()
  21. lbl_text2 = Label(Bottom, text="Display Result", font=('calibri', 14))
  22. lbl_text2.pack(side=TOP)
  23. lbl_result = Label(Bottom, font=('calibri', 12))
  24. lbl_result.pack()
  26. #=================================ENTRY WIDGETS=======================================
  27. txt_str = Entry(Right, font=('calibri', 14), textvariable=NUMBER)
  28. txt_str.pack()
  30. #=================================BUTTON WIDGETS======================================
  31. btn_convert = Button(Bottom, text="Convert", command=convert)
  32. btn_convert.pack(pady=10)

Creating the Main Function

This is where the code that contains the main functions. This code will convert the string into a readable integer data type when the button is clicked. To do that just copy and write these blocks of code.

  1. #=================================METHODS=============================================
  2. def convert():
  3. if NUMBER.get() == "":
  4. lbl_result.config(text="Please enter a string", fg="yellow")
  5. else:
  6. if num_verify(NUMBER.get()):
  7. integer = int(NUMBER.get())
  8. lbl_result.config(text="\"" + NUMBER.get() + "\" is a string, will be converted to " + str(integer) + " Integer type", fg="blue")
  9. else:
  10. lbl_result.config(text="This is not a string number", fg="Red")
  12. def num_verify(s):
  13. try:
  14. float(s)
  15. return True
  16. except ValueError:
  17. pass
  18. try:
  19. import unicodedata
  20. unicodedata.numeric(s)
  21. return True
  22. except (TypeError, ValueError):
  23. pass
  24. return False

Initializing the Application

After finishing the function save the application as This function will run the code and check if the main application is initialized 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 a Convert String To Integer Type 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!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.