Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

Python Program to Find the Area of a Rectangle

A short Python program calculates a rectangle’s area from length and width, with explanations for decimal inputs, output formatting, and reuse.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use area = length * width to calculate a rectangle’s area in Python. This interactive program accepts decimal dimensions, multiplies them, and prints the result:

length = float(input("Enter the length: "))
width = float(input("Enter the width: "))
area = length * width
print(f"The area of the rectangle is {area} square units.")

How the program works

  1. input() prompts the user and returns what they type as text. float(...) converts that text to a number that can include a decimal.
  2. The program stores each converted dimension in a clearly named variable: length and width.
  3. length * width applies the rectangle area formula and stores the result in area.
  4. print() displays a readable message. The f before the string makes it an f-string, so Python inserts the value of area where it appears inside braces.

Try an example

Enter 5 for the length and 3 for the width. The program prints The area of the rectangle is 15.0 square units. The decimal point appears because float represents the inputs as floating-point numbers.

As an Amazon Associate I earn from qualifying purchases.

Area uses square units. If the dimensions are measured in centimetres, the result is in square centimetres; if they are measured in metres, it is in square metres. The program does not ask for or infer a unit, so keep both dimensions in the same unit.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use two decimal places in the displayed result

To display the result with two digits after the decimal point, change the last line to:

print(f"The area of the rectangle is {area:.2f} square units.")

The :.2f format rounds the displayed value to two decimal places. It changes how the number is shown, not the value stored in area.

Make the calculation reusable with a function

If another part of your program needs to calculate rectangle areas, put the multiplication in a function:

def rectangle_area(length, width):
    return length * width

area = rectangle_area(5, 3)
print(area)

The function receives two values and returns their product. For a one-off calculation, the interactive program is simpler; a function is useful when you want to call the same calculation from different places.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Input limitations

This short program assumes the user enters numbers. Text such as five cannot be converted by float() and raises a ValueError. It also does not check that dimensions are positive; add input validation if your program needs to reject invalid measurements.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Run the code

Run the code in a Python interpreter, or save it in a text editor as a Python file and execute that file with your Python installation. The official Python tutorial’s interpreter section explains the interpreter and its use for trying examples; its tutorial introduction discusses preparing more involved programs in a text editor. The calculation uses Python’s multiplication operator, described in the informal introduction. For details on print() and f-strings, see the Python input and output tutorial.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.