DEV Community

JAYA SRI J
JAYA SRI J

Posted on

TASK 1

Hi all this is my task one with simple questions
1.Printing “Hello, world!”
concept: the print() function is used to print the output.

print("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

Explanation:
Anything inside quotes is a string. Python prints it exactly as it is.

2.Printing variable
Concept: Variables store values.

name = "Syed Jafer"
print(name)
Enter fullscreen mode Exit fullscreen mode

Explanation:
Instead of printing text directly, we store it in a variable and print it.

  1. Printing Multiple Variables with Labels Concept: Combine text with variables.
name = "Syed Jafer"
age = 20
city = "Chennai"
print("Name:", name)
print("Age:", age)
print("City:", city)
Enter fullscreen mode Exit fullscreen mode

Explanation:
Comma , automatically adds a space between values.

4.Using f-strings (Modern Method)
Concept: Clean and readable formatting.

print(f"Name: {name}, Age: {age}, City: {city}")
Enter fullscreen mode Exit fullscreen mode

Explanation:
f allows inserting variables inside {} directly.

  1. Concatenating Strings Concept: Joining strings using +.
greeting = "Hello"
target = "world"
print(greeting + " " + target)
Enter fullscreen mode Exit fullscreen mode

Explanation:
We manually add a space " " between words.

6.Printing Multiple Lines

Concept: Each print() gives a new line.

print("Line1")
print("Line2")
print("Line3")
Enter fullscreen mode Exit fullscreen mode

Explanation:
Every print statement moves to the next line automatically.

  1. Printing Quotes Inside String Concept: Mixing quotes safely.
print('He said, "Hello, world!"')
Enter fullscreen mode Exit fullscreen mode

Explanation:
Use single quotes outside to allow double quotes inside.

  1. Printing Backslashes (Raw String) Concept: Avoid escape sequences.
print(r"C:\Users\Name")
Enter fullscreen mode Exit fullscreen mode

Explanation:
r means raw string → Python ignores escape characters.

9.Printing Expression Result
Concept: Python can calculate inside print().

print(5 + 3)
Enter fullscreen mode Exit fullscreen mode

Explanation:
Python evaluates the expression first → prints 8.

  1. Printing with Hyphen Concept: Custom separators.
print("Hello-world")
Enter fullscreen mode Exit fullscreen mode

Explanation:
Hyphen is just a character inside the string.

  1. Printing on Same Line Concept: Control line ending.

print("Hello", end=" ")
print("world!")
Enter fullscreen mode Exit fullscreen mode

Explanation:
end=" " prevents newline and adds a space.

  1. Printing Boolean Value Concept: Boolean values (True/False).
is_active = True
print(is_active)
Enter fullscreen mode Exit fullscreen mode

Explanation:
Python prints boolean values directly.

  1. Repeating a String Concept: String multiplication.
print("Hello " * 3)
Enter fullscreen mode Exit fullscreen mode

Explanation:
"*" is used to multiple with 3 repeats the string three times.

  1. Using Variable in Sentence Concept: Dynamic text using variables.
temperature = 22.5
print(f"The temperature is {temperature} degrees Celsius.")

Enter fullscreen mode Exit fullscreen mode

Explanation:
Value is inserted inside the sentence.

  1. Using .format() Method Concept: Old-style formatting.
print("Name: {}, Age: {}, City: {}".format(name, age, city))
Enter fullscreen mode Exit fullscreen mode

Explanation:
{} are placeholders filled by .format().

  1. Rounding Numbers Concept: Formatting decimals.
pi = 3.14159
print("The value of pi is approximately {:.2f}".format(pi))

Enter fullscreen mode Exit fullscreen mode

Explanation:
:.2f → rounds to 2 decimal places.

  1. Alignment (Left & Right) Concept: Formatting width.
print("{:<10}{:>10}".format("left", "right"))
Enter fullscreen mode Exit fullscreen mode

Explanation:
<10 → left aligned

10 → right aligned

Top comments (0)