TASK-1 PYTHON PRINT EXERCISES
Python Print Exercises
Introduction
In Python, the print() function is used to display output on the screen. While it looks simple, there are multiple ways to print values depending on the situation. In this task, I explored different approaches and understood why certain methods are preferred over others.
1. Printing a simple string
print("Hello, world!")
Approach: Direct string printing
Why: This is the simplest way to display a fixed message. No variables are involved, so a direct string is enough.
2. Printing a variable
name = "Syed Jafer"
print(name)
Approach: Printing variable directly
Why: Instead of hardcoding values, we store data in variables. This makes the program flexible and reusable.
3. Printing variables with labels
name = "Syed Jafer"
age = 20
city = "Chennai"
print("Name:", name)
print("Age:", age)
print("City:", city)
Approach: Using commas in print()
Why: Python automatically adds space between values when using commas, making it simple and readable.
Alternative: String concatenation or formatting, but this is easier for beginners.
4. Using f-strings
print(f"Name: {name}, Age: {age}, City: {city}")
Approach: f-string formatting
Why chosen: f-strings are more readable and modern. They allow embedding variables directly inside strings.
Alternative: .format() or concatenation, but f-strings are faster and cleaner.
5. Concatenating strings
greeting = "Hello"
target = "world"
print(greeting + " " + target)
Approach: String concatenation
Why: Useful when combining multiple strings manually.
Limitation: Becomes messy when many variables are involved, so f-strings are often better.
6. Printing multiple lines
print("Line1")
print("Line2")
print("Line3")
Approach: Multiple print() statements
Why: Each print() moves to a new line automatically, making it straightforward.
7. Printing quotes inside a string
print('He said, "Hello, world!"')
Approach: Using single quotes outside
Why: This avoids escaping double quotes, making the code cleaner.
Alternative: Using escape character \".
8. Printing backslashes
print(r"C:\Users\Name")
Approach: Raw string
Why chosen: Prevents Python from interpreting \ as escape characters.
Alternative: Double backslashes (\\), but raw strings are simpler.
9. Printing expression result
print(5 + 3)
Approach: Direct expression inside print()
Why: Python evaluates the expression first and prints the result.
10. Using separator
print("Hello", "world", sep="-")
Approach: Using sep parameter
Why: Allows customization of separator without manual concatenation.
11. Printing on the same line
print("Hello", end=" ")
print("world!")
Approach: Using end parameter
Why: Prevents newline after first print and keeps output in one line.
12. Printing boolean value
is_active = True
print(is_active)
Approach: Direct variable printing
Why: Python automatically prints boolean values as True or False.
13. Repeating a string
print("Hello " * 3)
Approach: String multiplication
Why: Efficient way to repeat strings instead of writing multiple print statements.
14. Using variables in a sentence
temperature = 22.5
print(f"The temperature is {temperature} degrees Celsius.")
Approach: f-string
Why: Clean and readable way to include variables in sentences.
15. Using .format()
print("Name: {}, Age: {}, City: {}".format(name, age, city))
Approach: .format() method
Why: Works in older Python versions and is flexible.
Comparison: f-strings are preferred in modern Python due to better readability.
16. Formatting decimal values
pi = 3.14159
print(f"The value of pi is approximately {pi:.2f}")
Approach: f-string formatting
Why: Controls decimal precision easily and cleanly.
17. Aligning text
print(f"{'left':<10}{'right':>10}")
Approach: String alignment in f-strings
Why: Helps format output neatly, useful for tables and structured display.
Conclusion
In this task, different approaches of printing in Python were explored. While multiple methods exist, choosing the right approach depends on readability, simplicity, and the situation. Modern methods like f-strings are generally preferred because they make the code cleaner and easier to understand.
Comments
Post a Comment