Python Basics: The print() Function and String Formatting

Welcome back to our Python foundational series! In our previous session, we learned how to define variables and store different data types in memory. However, to make our programs interactive and to understand what is happening under the hood, we need a way to display this data.

Whether you are printing a simple greeting or outputting the training loss of a complex Deep Learning model, the print() function is your primary mechanism for communicating with the standard output. In this session, we will explore the various, powerful ways to print and format variable values in Python.


Printing Variable Values

Python offers several methodologies to format and print text mixed with variables. As a software engineer or data scientist, you must be familiar with all of them, as you will encounter different styles in various codebases.

1. Using the print() Function (Basic)

The most straightforward way to output data is by passing multiple arguments to the print() function, separated by commas. When you do this, Python automatically converts the arguments to strings and inserts a space between them.

name = "Python"
age = 30

print("My favorite language is", name)  # Output: My favorite language is Python
print("I am", age, "years old")         # Output: I am 30 years old

2. Printing with Concatenation

Another traditional method is concatenation, which involves “adding” strings together using the + operator.

Crucial Note: You can only concatenate strings with other strings. If you attempt to concatenate a string with an integer (like age), Python will throw a TypeError. Therefore, you must explicitly convert the integer to a string using the str() casting function.

# Concatenation with `+`
name = "Ali"
age = 20

print("Name: " + name)                  # Output: Name: Ali
print("Age: " + str(age))               # Output: Age: 20

3. Using f-Strings (Formatted String Literals)

Introduced in Python 3.6, f-strings are widely considered the most modern, readable, and efficient way to format strings in Python. By placing an f or F directly before the opening quotation mark, you can embed Python variables directly inside curly braces {}.

This is the industry-standard approach for modern Data Science and AI development.

# Using f-strings
name = "Ali"
age = 20

print(f"My name is {name} and I am {age} years old.")  
# Output: My name is Ali and I am 20 years old.

4. Using the format() Method

Before f-strings were introduced, the .format() method was the preferred standard. It uses empty curly braces {} as placeholders within the string. The variables passed into the .format() method are then injected into those placeholders in order. You will see this frequently in slightly older codebases.

# Using format() method
name = "Ali"
age = 20

print("My name is {} and I am {} years old.".format(name, age))  
# Output: My name is Ali and I am 20 years old.

5. Printing with Inline Expressions

One of the most powerful features of f-strings is that they do not just accept variables; they can evaluate entire Python expressions dynamically inline. You can perform mathematics, call functions, or execute logic directly within the curly braces.

# Inline expressions with f-strings
a = 5
b = 10

print(f"The sum of {a} and {b} is {a + b}.")  
# Output: The sum of 5 and 10 is 15.

6. Printing Variables with Escape Characters

Sometimes, you need to format the structural layout of your output—adding new lines, tabs, or inserting quotation marks inside a string. Python uses the backslash \ as an escape character to invoke special behaviors.

Here is a comprehensive list of common escape sequences in action:

# \n : New Line - Moves text to the next line
print("Hello\nWorld")  
# Output:
# Hello
# World

# \t : Tab - Adds a horizontal space (tab) in the text
print("Name:\tAli")  
# Output: Name:   Ali

# \b : Backspace - Deletes the character immediately before it
print("Helloo\b World")  
# Output: Hello World

# \' and \" : Single/Double Quotes - Allows usage of quotes inside strings without breaking the syntax
print("He said, \"Hello!\"")   
# Output: He said, "Hello!"

print('It\'s a beautiful day')  
# Output: It's a beautiful day

# \\ : Backslash - Displays the backslash character itself (vital for Windows file paths)
print("Path: C:\\Users\\Ali")  
# Output: Path: C:\Users\Ali

# \a : Alert/Bell - Triggers the system alert (This may produce a beep sound depending on your OS terminal)
print("Alert\a")  

Conclusion

Mastering the print() function and string formatting is an essential stepping stone. As you progress into building Machine Learning pipelines, you will heavily rely on f-strings and escape characters to generate clean, readable logs detailing your model’s accuracy, epochs, and error rates.

In our next session, we will dive deeper into Python Operators and Type Casting. Keep practicing these string formats, and happy coding!

Notebook

Open In Colab

📄 View or Download the Python Notebook




Enjoy Reading This Article?

Here are some more articles you might like to read next: