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,...

Escape Sequences in Java: A Beginner’s Guide with Examples

When working with strings and characters in Java, you may come across situations where you need to represent special characters that cannot be typed directly into code. For example, how do you include a quotation mark inside a string? Or how do you move text to a new line?

This is where escape sequences come into play.


What are Escape Sequences?

Escape sequences are special character combinations that start with a backslash (\) followed by a character. They allow us to represent characters that otherwise would be difficult or impossible to include directly in a string or character literal.

In simple words, escape sequences help Java understand:

  • How to display special symbols.

  • How to handle formatting like new lines or tabs.


Common Escape Sequences in Java

Here’s a list of frequently used escape sequences in Java:

Escape Sequence Description Example Output
\n Inserts a new line "Hello\nWorld" → Hello World
\t Inserts a tab space "Hello\tWorld" → Hello World
\" Prints double quotes inside a string "She said, \"Hello!\"" → She said, "Hello!"
\' Prints a single quote '\'A\'' → 'A'
\\ Prints a backslash "C:\\Program Files" → C:\Program Files
\r Carriage return (moves cursor to start of line) Output depends on system
\b Backspace (removes previous character) "Hello\bWorld" → HeloWorld
\f Form feed (page break, rarely used) Behavior depends on system

Example Program in Java

public class EscapeSequenceExample {
    public static void main(String[] args) {
        System.out.println("Hello\nWorld");     // New line
        System.out.println("Hello\tWorld");     // Tab space
        System.out.println("She said, \"Java is fun!\"");  // Quotes
        System.out.println("Path: C:\\Java\\bin");         // Backslash
    }
}

Output:

Hello
World
Hello    World
She said, "Java is fun!"
Path: C:\Java\bin

Why Use Escape Sequences?

  • To format output (e.g., new lines, indentation).

  • To represent characters not directly typeable (quotes, backslashes).

  • To make strings readable when special characters are required.


Conclusion

Escape sequences in Java are small but powerful tools that make text formatting and handling special characters easier. By mastering them, you can create more readable, user-friendly, and well-formatted programs.

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...