Session 1 · The print() function#

📖 Based on the blog post Python Fundamentals: The Print() and Task 1: Print exercises.

📺 Watch on YouTube (Tamil)

The big idea#

The first thing you learn in any language is how to show something on the screen. In Python that’s print(). It looks simple, but it has a few tricks that you’ll use every day, from greeting a user to finding bugs.

Everyday example: a shop’s display board. Whatever the shopkeeper writes on it, customers see. print() is your program’s display board.

Printing text, variables and numbers#

Text goes inside quotes, single or double:

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

Variables and numbers go in without quotes. Python prints their value:

name = "Parotta Salna"
print(name)
print(12345)
print(5 + 3)
Parotta Salna
12345
8

Several items at once#

Separate items with commas. Python puts a space between them:

name = "Parotta Salna"
age = 25
city = "Chennai"
print("Name:", name, "Age:", age, "City:", city)
Name: Parotta Salna Age: 25 City: Chennai
What print() does with its arguments print() turns each argument into text, puts sep between them (a space by default) and end after the last one (a newline by default). "Hello" sep "world" end print("Hello", "world") → Hello world⏎ print("Hello", "world", sep="-", end="!") → Hello-world! defaults: sep=" " end="\n"
print() turns each argument into text, puts sep between them (a space by default) and end after the last one (a newline by default).

sep and end#

sep is what goes between items (a space by default). end is what goes after the last one (a newline by default):

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

Four ways to mix text and values#

name, age, city = "Parotta Salna", 25, "Chennai"

print(f"Name: {name}, Age: {age}, City: {city}")                 # 1. f-string (recommended)
print("Name: {}, Age: {}, City: {}".format(name, age, city))      # 2. .format()
print("Name: " + name + ", City: " + city)                        # 3. + (strings only)
print("Name:", name, "Age:", age)                                 # 4. commas
Name: Parotta Salna, Age: 25, City: Chennai
Name: Parotta Salna, Age: 25, City: Chennai
Name: Parotta Salna, City: Chennai
Name: Parotta Salna Age: 25

+ only joins strings. To add a number, convert it with str() first:

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

f-strings can also format values: :.2f keeps two decimals, :<10 / :>10 pad to a width.

pi = 3.14159
print(f"The value of pi is approximately {pi:.2f}")
print(f"{'left':<10}|{'right':>10}|")
The value of pi is approximately 3.14
left      |     right|

Special characters#

You write

You get

"Line1\nLine2"

a new line between them (\n)

"Name\tAge"

a tab (\t)

'He said, "Hello!"'

double quotes inside single quotes

r"C:\Users\Name"

a raw string: backslashes printed as they are

"""three\nlines"""

triple quotes: a string over several lines

print("Line1\nLine2\nLine3")
print('He said, "Hello, world!"')
print(r"C:\Users\Name")
Line1
Line2
Line3
He said, "Hello, world!"
C:\Users\Name

Printing other things#

print("Hello " * 3)                                   # repeat a string
print(True, None)                                     # booleans and None
print(["apple", "banana", "cherry"])                  # a list
print({"name": "Alice", "age": 25})                   # a dictionary
for i in range(3):                                    # in a loop
    print("Iteration", i)
Hello Hello Hello 
True None
['apple', 'banana', 'cherry']
{'name': 'Alice', 'age': 25}
Iteration 0
Iteration 1
Iteration 2

Common mistakes#

  • Print("hi"): Python is case-sensitive. It’s print, all lowercase.

  • Forgetting quotes: print(Hello) looks for a variable called Hello → NameError.

  • "Age: " + 25: you can’t + a string and a number → TypeError. Use an f-string or str(25).

  • Windows paths: in "C:\new_folder", \n becomes a new line! Use r"C:\new_folder".

Hands-on exercises#

These are the 17 tasks from Task 1. Try each one before opening the solution.

📺 Solutions video on YouTube (Tamil)

Task 1. Print the string “Hello, world!”.

Solution
print("Hello, world!")

Task 2. Print the value of a variable name set to your name.

Solution
name = "Syed Jafer"
print(name)

Task 3. Print name, age and city with the labels “Name:”, “Age:” and “City:”.

Solution
name, age, city = "Syed Jafer", 25, "Chennai"
print("Name:", name, "Age:", age, "City:", city)

Task 4. Use an f-string to print them as “Name: …, Age: …, City: …”.

Solution
name, age, city = "Syed Jafer", 25, "Chennai"
print(f"Name: {name}, Age: {age}, City: {city}")

Task 5. Join greeting = "Hello" and target = "world" with a space between and print it.

Solution
greeting, target = "Hello", "world"
print(greeting + " " + target)

Task 6. Print “Line1”, “Line2” and “Line3” on separate lines, with one print.

Solution
print("Line1\nLine2\nLine3")

Task 7. Print He said, "Hello, world!" including the double quotes.

Solution
print('He said, "Hello, world!"')
# or escape them: print("He said, \"Hello, world!\"")

Task 8. Print C:\Users\Name without escaping the backslashes.

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

Task 9. Print the result of 5 + 3.

Solution
print(5 + 3)

Task 10. Print “Hello” and “world” separated by a hyphen.

Solution
print("Hello", "world", sep="-")

Task 11. Print “Hello” followed by a space, then “world!” on the same line, using two print calls.

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

Task 12. Print a boolean variable is_active set to True.

Solution
is_active = True
print(is_active)

Task 13. Print “Hello “ three times in a row.

Solution
print("Hello " * 3)

Task 14. Print The temperature is 22.5 degrees Celsius. using a variable temperature.

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

Task 15. Print name, age and city using .format().

Solution
name, age, city = "Syed Jafer", 25, "Chennai"
print("Name: {}, Age: {}, City: {}".format(name, age, city))

Task 16. Print pi (3.14159) rounded to two decimals: The value of pi is approximately 3.14.

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

Task 17. Print “left” left-aligned and “right” right-aligned, each within 10 characters.

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

Output: left           right (each word padded to 10 characters).