Skip to main content

Python print() Function

When you start learning Python, one of the first things you need to learn is how to display information on the screen. For example, you might want to display: A welcome message. Your name and age. The result of a mathematical calculation. Student marks and results. Information generated by a Python program. Python provides a built-in function called print() for displaying information. The print() function is one of the simplest and most frequently used functions in Python. It helps programmers display information while testing programs, solving problems, and developing applications. In this article, we will learn how to use print() with practical examples and their outputs. 1. What Is the print() Function in Python? The print() function is a built-in Python function that displays information in the output area, usually the terminal or console. Let's look at a simple example. print("Hello, World!") Output: Hello, World! In this example, Python displays the text Hello,...

HTML Forms – Collecting User Input on Webpages

HTML forms are one of the most important parts of web development. Forms allow users to enter and submit information to websites.

Forms are used everywhere on the internet, including:

  • Login pages

  • Registration forms

  • Search bars

  • Feedback forms

  • Online shopping websites

  • Banking websites

  • Admission forms

In this article, we will learn:

  • What HTML forms are

  • Form structure

  • Form elements

  • Input types

  • Labels

  • Buttons

  • Dropdown lists

  • Radio buttons

  • Checkboxes

  • Textareas

  • Form attributes

  • Form validation

  • Practical examples


What is an HTML Form?

An HTML form is used to collect user data.

The <form> tag creates a form.

Users can enter:

  • Text

  • Passwords

  • Email

  • Numbers

  • Dates

  • Files

  • Selections

The data is then submitted to a server for processing.


Basic Structure of a Form

<form>

</form>

All form elements are placed inside the <form> tag.


Simple Form Example

<!DOCTYPE html>
<html>

<head>
    <title>Simple Form</title>
</head>

<body>

    <h1>Student Registration</h1>

    <form>

        Name:
        <input type="text">

        <br><br>

        <input type="submit">

    </form>

</body>

</html>

Output Structure

Student Registration

Name: [___________]

[Submit]

The <input> Tag

The <input> tag is the most commonly used form element.

Syntax:

<input type="value">

The type attribute defines the kind of input.


Common Input Types

Input TypePurpose
textSingle-line text
passwordHidden password
emailEmail input
numberNumeric input
radioSingle selection
checkboxMultiple selections
submitSubmit button
resetReset form
fileUpload files
dateDate picker
colorColor picker

1. Text Input

Used for entering text.

Example:

<input type="text">

Example Program

<form>

    Enter Name:
    <input type="text">

</form>

2. Password Input

Password text becomes hidden.

Example:

<input type="password">

Example

<form>

    Password:
    <input type="password">

</form>

3. Email Input

Accepts email addresses.

Example:

<input type="email">

Browsers may validate proper email format.


4. Number Input

Used for numeric values.

Example:

<input type="number">

5. Radio Buttons

Radio buttons allow selecting only one option.

Example:

<input type="radio" name="gender"> Male

<input type="radio" name="gender"> Female

Important:

  • Same name groups radio buttons together.


Example Program

<form>

    Gender:

    <input type="radio" name="gender">
    Male

    <input type="radio" name="gender">
    Female

</form>

6. Checkboxes

Checkboxes allow multiple selections.

Example:

<input type="checkbox"> Cricket

<input type="checkbox"> Football

Example Program

<form>

    Hobbies:

    <input type="checkbox"> Music

    <input type="checkbox"> Reading

    <input type="checkbox"> Gaming

</form>

7. Submit Button

Used to submit form data.

Example:

<input type="submit">

8. Reset Button

Resets all form values.

Example:

<input type="reset">

9. File Upload

Allows users to upload files.

Example:

<input type="file">

10. Date Input

Displays a date picker.

Example:

<input type="date">

11. Color Picker

Allows selecting colors.

Example:

<input type="color">

Placeholder Attribute

Displays temporary hint text.

Example:

<input type="text"
       placeholder="Enter your name">

Value Attribute

Sets default input value.

Example:

<input type="text"
       value="Rahul">

Readonly Attribute

Prevents editing.

Example:

<input type="text"
       value="India"
       readonly>

Disabled Attribute

Disables input field.

Example:

<input type="text" disabled>

Required Attribute

Makes input mandatory.

Example:

<input type="text" required>

Labels in Forms

The <label> tag improves accessibility.

Example:

<label>Name:</label>

<input type="text">

Label with for Attribute

<label for="username">
    Username
</label>

<input type="text" id="username">

Textarea

Used for multi-line input.

Example:

<textarea rows="5" cols="30">

</textarea>

Example

<form>

    Address:

    <br>

    <textarea rows="4"
              cols="40"></textarea>

</form>

Dropdown List

Created using:

  • <select>

  • <option>


Example

<select>

    <option>India</option>

    <option>USA</option>

    <option>Japan</option>

</select>

Complete Dropdown Example

<form>

    Select Course:

    <select>

        <option>BCA</option>

        <option>BBA</option>

        <option>BTech</option>

    </select>

</form>

Form Buttons

HTML supports buttons using:

  • <button>

  • <input type="button">

Example:

<button>Click Me</button>

Form Attributes

Important form attributes:

AttributePurpose
actionForm submission URL
methodHTTP method
autocompleteEnables suggestions
targetWhere response opens

Action Attribute

Defines where form data goes.

Example:

<form action="submit.php">

Method Attribute

Defines submission method.

MethodPurpose
GETVisible data in URL
POSTSecure data transfer

Example

<form method="POST">

GET vs POST

FeatureGETPOST
Data VisibleYesNo
SecurityLessMore
URL Length LimitYesNo

Autocomplete Attribute

Example:

<form autocomplete="on">

Browser suggests previous values.


Complete Registration Form

<!DOCTYPE html>
<html>

<head>
    <title>Registration Form</title>
</head>

<body>

    <h1>Student Registration Form</h1>

    <form>

        Name:
        <input type="text"
               placeholder="Enter Name"
               required>

        <br><br>

        Password:
        <input type="password">

        <br><br>

        Email:
        <input type="email">

        <br><br>

        Gender:

        <input type="radio"
               name="gender"> Male

        <input type="radio"
               name="gender"> Female

        <br><br>

        Hobbies:

        <input type="checkbox"> Music

        <input type="checkbox"> Sports

        <br><br>

        Course:

        <select>

            <option>BCA</option>

            <option>BBA</option>

            <option>BTech</option>

        </select>

        <br><br>

        Address:

        <br>

        <textarea rows="5"
                  cols="40"></textarea>

        <br><br>

        <input type="submit">

        <input type="reset">

    </form>

</body>

</html>

Output Structure

Student Registration Form

Name: [___________]

Password: [___________]

Email: [___________]

Gender:
( ) Male
( ) Female

Hobbies:
[ ] Music
[ ] Sports

Course:
[BCA ▼]

Address:
[_______________]

[Submit] [Reset]

HTML5 Form Features

HTML5 introduced advanced inputs:

  • Email validation

  • Date picker

  • Range slider

  • Color picker

  • URL input

Modern browsers support these features automatically.


Form Validation

Validation ensures proper user input.

Example:

<input type="email" required>

Browser checks:

  • Empty field

  • Invalid email format


Advantages of HTML Forms

  • Collect user information

  • Enable user interaction

  • Support registration and login

  • Essential for web applications

  • Easy to use


Real-Life Uses of Forms

Forms are used in:

  • Online shopping websites

  • Banking systems

  • Social media platforms

  • Admission portals

  • Job applications

Platforms like Facebook, Google, and Amazon use forms extensively for login, registration, and user interaction.


Common Mistakes Beginners Make

1. Forgetting Name Attribute

Wrong:

<input type="text">

Better:

<input type="text" name="username">

2. Missing Labels

Forms become difficult to understand without labels.


3. Using GET for Passwords

Never send sensitive data using GET.


4. Forgetting Required Validation

Users may submit incomplete forms.


Conclusion

HTML forms are the foundation of interactive websites. They allow users to send data, register accounts, login, search, and communicate with websites.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? ๐Ÿ“Œ Readability – Comments make code easier to understand for you and others. ๐Ÿ“Œ Maintenance – When you revisit your code after months, comments remind you what you were doing. ๐Ÿ“Œ Collaboration – In team projects, comments explain code to fellow developers. ๐Ÿ“Œ Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . ๐Ÿ’ป Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } ๐Ÿง  Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

๐Ÿ’ก Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. ๐Ÿง‘‍๐Ÿ’ป Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...