Parottasalna

AI, Backend Engineering & Architecture Guides

Python-FUNDAMENTALS: The Print()

Welcome to the world of ParottaSalna !

One of the first things you’ll learn in any programming language is how to display output on the screen.

In Python, we do this using the print function. It’s simple, yet powerful. Let’s explore the various ways you can use print in Python.

1. The Basics: Printing a String

To print text, you simply put the text inside double or single quotes and pass it to the print function.

print("Hello, world!")

This will display: Hello, world!

2. Printing Variables

Variables are used to store data. You can print variables by passing them to the print function.

name = "Parotta Salna"
print(name)

This will display: `Parotta Salna`

3. Printing Multiple Items

You can print multiple items by separating them with commas. Python will add a space between each item.

age = 25
city = "New York"
name = "Parotta Salna"
print("Name:", name, "Age:", age, "City:", city)

This will display: Name: Parotta Salna Age: 25 City: New York

4. Formatted Strings with f-strings

An f-string is a way to format strings in Python. You can insert variables directly into the string by prefixing it with an f and using curly braces {} around the variables.

age = 25
city = "New York"
name = "Parotta Salna"
print(f"Name: {name}, Age: {age}, City: {city}")

This will display: Name: Parotta Salna Age: 25 City: New York

5. Concatenation of Strings

You can also combine (concatenate) strings using the + operator.

main_dish = "Idly"
side_dish = "Sambar"
print(main_dish + " " + side_dish + "!")

This will display: Idly Sambar!

6. Using Escape Sequences

Escape sequences allow you to include special characters in a string. For example, \n adds a new line.

print("Line1\nLine2\nLine3")

This will display:

7. Printing Quotes Inside Strings

To print quotes inside a string, you can use either single or double quotes to enclose the string and the other type of quotes inside it.

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

This will display: He said, “Hello, world!”

8. Raw Strings to Ignore Escape Sequences

Prefix the string with r to treat backslashes as literal characters.

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

This will display: C:\Users\Name

9. Printing Numbers

You can print numbers directly without quotes.

print(12345)

This will display: 12345

10. Printing Results of Expressions

You can also print the result of an expression.

print(5 + 3)

This will display: 8

11. Printing Lists and Dictionaries

You can print entire lists and dictionaries.

fruits = ["apple", "banana", "cherry"]
print(fruits)

person = {"name": "Alice", "age": 25, "city": "New York"}
print(person)

This will display:

[‘apple’, ‘banana’, ‘cherry’]

{‘name’: ‘Alice’, ‘age’: 25, ‘city’: ‘New York’}

12. Using sep and end Parameters

The sep parameter changes the separator between items, and end changes the ending character (default is newline).

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

This will display: Hello-world!

13. Multiline Strings with Triple Quotes

Triple quotes allow you to print multiline strings easily.

print("""This is a
multiline
string""")

This will display:

14. Printing in a Loop

You can use a loop to print multiple lines.

for i in range(5):
    print("Iteration", i)

15. String Multiplication

Multiply a string by an integer to repeat it.

print("Hello " * 3)

This will display: Hello Hello Hello

16. Printing Boolean Values

Print boolean values directly.

is_active = True
print(is_active)

This will display: True

17. Printing None

Print the None value directly.

value = None
print(value)

This will display: None

18. Combining Strings and Variables

Combine strings and variables using + for simple cases or formatted strings for more complex scenarios.

temperature = 22.5
print("The temperature is " + str(temperature) + " degrees Celsius.")

This will display: The temperature is 22.5 degrees Celsius.

19. Using print for Debugging

You can use print to debug your code by printing variable values at different points.

def add(a, b):
    print(f"Adding {a} and {b}")
    return a + b

result = add(5, 3)
print("Result:", result)

This will display:





20. Printing with .format()

Use the .format() method for string formatting.

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

This will display: Name: Alice, Age: 25, City: New York

Excercises:

Quiz:

https://docs.google.com/forms/d/e/1FAIpQLSeW7dGCYrvPXBK7llexbwa_yImFQWFiHHE4c4ATOk-NwJWxIw/viewform?usp=sf_link

Infographics:

Discover more from Parottasalna

Subscribe now to keep reading and get access to the full archive.

Continue reading