In Python, the print() function is very flexible—it can handle almost any data type automatically. You don't usually need to tell Python what type of data you are printing; the interpreter figures it out for you.
Here is how you print the most common data types:
1. Basic Data Types
You can pass them directly into the function:
print("Hello") # String (Text)
print(123) # Integer (Whole number)
print(3.14) # Float (Decimal)
print(True) # Boolean (True/False)
2. Printing Multiple Types Together
The easiest way to print different types in one line is to separate them with a comma (,). Python will convert them all to text and join them with a space:
age = 25
print("I am", age, "years old.")
# Output: I am 25 years old.
3. Converting Types (Casting)
If you try to "add" a string and a number using the + symbol, Python will give you an error. To fix this, you can convert the data type using str():
# This would cause an error: print("Score: " + 100)
print("Score: " + str(100)) # Works! Changes 100 to "100"
4. Advanced: f-Strings (The Modern Way)
As you get more comfortable, you can use f-strings. You put an f before the quotes and place your variables inside curly braces {}. This is the cleanest way to mix types:
name = "Labby"
version = 3.10
print(f"I am {name}, running on Python {version}")
# Output: I am Labby, running on Python 3.10
Try this in your interpreter right now:
my_list = [1, 2, 3]
my_dict = {"goal": "learn"}
print("List:", my_list, "and Dictionary:", my_dict)
You'll see that Python even prints complex collections like lists and dictionaries perfectly