DEV Community

Haripriya V
Haripriya V

Posted on

TASK 1- Python – Print exercises

  1. How do you print the string " Hello, world!" to the screen?

CODE:

print("Hello, world!")

OUTPUT:

Hello, world!

EXPLANATION:

  • print() is a built-in function in Python

  • It is used to display output on the screen

  • The text inside quotes (" ") is called a string

  • Python prints exactly what is inside the quotes

2.How do you print the value of a variable name which is set to “Syed Jafer” or Your name?

CODE:

name = "Haripriya"
print(name)

OUTPUT:

Haripriya

EXPLANATION:

  • name is a variable that stores a string value

  • "Haripriya" is the value assigned to the variable

  • print(name) displays the value stored in the variable

  • Python prints the content of the variable, not the word name

3.How do you print the variables name, age, and city with labels “Name:”, “Age:”, and “City:”?

CODE:

`name = "Haripriya"
age = 18
city = "Namakkal"

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

OUTPUT:

Name: Haripriya
Age: 18
City: Namakkal

EXPLANATION:

  • Three variables are created: name, age, and city

  • print() is used to display output

  • You can print both text (labels) and variables together using commas

  • Python automatically adds a space between them

4.How do you use an f-string to print name, age, and city in the format “Name: …, Age: …, City: …”?

CODE:

`name = "Haripriya"
age = 18
city = "Namakkal"

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

OUTPUT:

Name: Haripriya, Age: 18, City: Namakkal

EXPLANATION:

  • An f-string is created by adding f before the string

  • Variables are placed inside {} brackets

  • Python replaces {name}, {age}, {city} with their values

  • Everything prints in one line with proper formatting

4.How do you concatenate and print the strings greeting (“Hello”) and target (“world”) with a space between them?

CODE:

`greeting = "Hello"
target = "world"

print(greeting + " " + target)`

OUTPUT:

Hello world

EXPLANATION:

    • is used to concatenate (join) strings
  • " " adds a space between the two words

  • The final result becomes "Hello world"

6.How do you print three lines of text with the strings “Line1”, “Line2”, and “Line3” on separate lines?

CODE:

print("Line1\nLine2\nLine3")

OUTPUT:

Line1
Line2
Line3

EXPLANATION:

  • \n is a newline character that forces text onto the next line

7.How do you print the string He said, "Hello, world!" including the double quotes?

CODE:

print('He said, "Hello, world!"')

OUTPUT:

He said, "Hello, world!"

EXPLANATION:

  • The string is enclosed in single quotes ' '

  • This allows you to use double quotes " " inside the string without error

  • Python prints everything exactly as written

8.How do you print the string C:\Users\Name without escaping the backslashes?

CODE:

print(r"C:\Users\Name")

OUTPUT:

C:\Users\Name

EXPLANATION:

  • r before the string makes it a raw string

  • In raw strings, backslashes are treated as normal characters

  • No need to use \ for escaping

9.How do you print the result of the expression 5 + 3?

CODE:

print(5 + 3)

OUTPUT:

8

EXPLANATION:

  • 5 + 3 is an arithmetic expression

    • is the addition operator
  • Python evaluates the expression first → 5 + 3 = 8

  • Then print() displays the result

10.How do you print the strings “Hello” and “world” separated by a hyphen -?

CODE:

print("Hello-world")

OUTPUT:

Hello-world

EXPLANATION:

  • The hyphen - is added directly inside the string

-Python prints the string exactly as written

11.How do you print the string “Hello” followed by a space, and then print “world!” on the same line?

CODE:

print("Hello", end=" ")
print("world!")

OUTPUT:

Hello world!

EXPLANATION:

  • By default, print() moves to a new line after printing

  • The end=" " argument changes this behavior

  • It tells Python to end the first print with a space instead of a newline

  • So the next print() continues on the same line

12.How do you print the value of a boolean variable is_active which is set to True?

CODE:

is_active = True
print(is_active)

OUTPUT:

True

EXPLANTION:

  • is_active is a boolean variable

  • It stores either True or False

  • print(is_active) displays the value stored in the variable

  • Python prints boolean values exactly as True or False

13.How do you print the string “Hello ” three times in a row?

CODE:

print("Hello " * 3)

OUTPUT:

Hello Hello Hello

EXPLANATION:

    • is used for string repetition
  • "Hello " * 3 repeats the string 3 times

  • Python combines them into one continuous string

14.How do you print the sentence The temperature is 22.5 degrees Celsius. using the variable temperature?

CODE:

temperature = 22.5
print(f"The temperature is {temperature} degrees Celsius.")

OUTPUT:

The temperature is 22.

EXPLANATION:

  • temperature is a variable that stores the value 22.5

  • The f before the string makes it an f-string

  • {temperature} is a placeholder inside the string

  • Python replaces {temperature} with the actual value 22.5

  • The final output becomes a complete sentence with the value inserted

15.How do you print name, age, and city using the .format() method in the format “Name: …, Age: …, City: …”?

CODE:

`name = "Haripriya"
age = 18
city = "Namakkal"

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

OUTPUT:

Name: Haripriya, Age: 18, City: Namakkal

EXPLANATION:

  • {} are placeholders inside the string

  • .format(name, age, city) replaces each {} with corresponding values

  • The values are inserted in the same order as given

  • This method was commonly used before f-strings

16.How do you print the value of pi (3.14159) rounded to two decimal places in the format The value of pi is approximately 3.14?

CODE:

pi = 3.14159
print(f"The value of pi is approximately {pi:.2f}")

OUTPUT:

The value of pi is approximately 3.14

EXPLANATION:

  • pi is a variable storing the value 3.14159

  • f makes it an f-string

  • {pi:.2f} formats the number:

    • .2 → rounds to 2 decimal places
    • f → formats it as a floating-point number
  • Python rounds 3.14159 to 3.14

17.How do you print the words “left” and “right” with “left” left-aligned and “right” right-aligned within a width of 10 characters each?

CODE:

print(f"{'left':<10}{'right':>10}")

OUTPUT:

left right

EXPLANATION:

  • :<10 → left-align within 10 spaces

  • :>10 → right-align within 10 spaces

  • 'left' occupies the left side of its 10-character space

  • 'right' is pushed to the right side of its 10-character space

  • Both are printed on the same line

Top comments (0)