DEV Community

Santhoshi Mary A
Santhoshi Mary A

Posted on

Python-print exercises

1️. How to print the string “Hello, world!”
print("Hello, world!")

Explanation:
Strings must be written inside quotes (" " or ' ').
The print() function displays the text exactly as written.

2️. How to print the value of a variable
name = "Syed Jafer"
print(name)

Explanation:
We store the string inside a variable called name.
Printing the variable displays its stored value.

3️. Print name, age, and city with labels
name = "Syed Jafer"
age = 20
city = "Chennai"

print("Name:", name)
print("Age:", age)
print("City:", city)

Explanation:
Commas separate multiple values.
Python automatically adds a space between them.

4️. Using f-string formatting
name = "Syed Jafer"
age = 20
city = "Chennai"

print(f"Name: {name}, Age: {age}, City: {city}")

Explanation:

f before the string enables formatting.
{} allows variables to be inserted.
This is the modern and clean method.
5️. Concatenate two strings with a space
greeting = "Hello"
target = "world"

print(greeting + " " + target)

Explanation:
The + operator joins strings.
" " adds a space between them.

6️. Print three lines separately
print("Line1")
print("Line2")
print("Line3")
OR using newline character:
print("Line1\nLine2\nLine3")

Explanation:
\n represents a newline.

7️. Print double quotes inside a string
print('He said, "Hello, world!"')

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

8️. Print C:\Users\Name without escaping backslashes
print(r"C:\Users\Name")

Explanation:
r makes it a raw string.
Backslashes are treated as normal characters.

9️. Print the result of 5 + 3
print(5 + 3)

Explanation:
Python evaluates the expression first.
Output will be: 8

  1. Print “Hello” and “world” separated by a hyphen print("Hello", "world", sep="-")

Explanation:
sep changes the separator between values.
Default separator is space.

1️1. Print on the same line
print("Hello", end=" ")
print("world!")

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

1️2. Print a boolean variable
is_active = True
print(is_active)

Explanation:
Boolean values (True or False) are printed directly.

1️3. Print “Hello ” three times
print("Hello " * 3)

Explanation:

  • repeats a string multiple times. Output: Hello Hello Hello

1️4. Print temperature using a variable
temperature = 22.5
print(f"The temperature is {temperature} degrees Celsius.")

Explanation:
We use an f-string to insert the variable inside the sentence.

1️5. Using .format() method
name = "Syed Jafer"
age = 20
city = "Chennai"

print("Name: {}, Age: {}, City: {}".format(name, age, city))

Explanation:
{} are placeholders.
.format() fills them in order.

1️6. Round pi to 2 decimal places
pi = 3.14159
print(f"The value of pi is approximately {pi:.2f}")

Explanation:
:.2f means:

2 → two decimal places
f → float format

Output: 3.14

  1. Left and Right Alignment (Width 10) print(f"{'left':<10}{'right':>10}")

Explanation:

<10 → left align within 10 spaces

10 → right align within 10 spaces

Top comments (0)