CodeBlessU
learn code with examples

Python Print Data Types

If you are looking for simple, general approach to print commonly used data types, use .format(). You do not need to convert using str() and it will support precision for float.

Syntax:
print("Your message {}, {}".format(variable1, variable2) )

Use {} for each variable to be printed within string, it will be replaced by value on screen. Apply .format() method and provide variable names.

# Example, print using .format() simple way
a1 = False
a2 = True
a3 = -1000
a4 = 10.55555555555
a5 = "String Message"
a6 = [1, 1.1, "Message"]
a7 = []
a8 = (1, 2, 10.0)
a9 = tuple()
a10 = {1, 10, 100}
a11 = set()
a12 = {1: "Apple", 2: "Ball" }
a13 = {}

print("Simple print: {}".format(a3) )

print("Values are, a1: {}, a2: {}, a3: {:2d}, a4: {:8.2f}, a5: {}".format(a1, a2, a3, a4, a5) )

print("List print a6: {}, ................. Empty List print a7: {}".format(a6, a7) )

print("Tuple print a8: {}, ................. Empty Tuple print a9: {}".format(a8, a9) )

print("Set print a10: {}, ................. Empty Set print a11: {}".format(a10, a11) )

print("Dict print a12: {}, ................. Empty Dict print a13: {}".format(a12, a13) )

Print Boolean

This simple approach is recommended for printing commonly used data types.

To print bool data type, Use string concatenation + operator and str() function. This is used to print bool with some other string message.

# Example, print bool
a1 = False
print("Value of a1: " + str(a1) )

a1 = True
print("Value of a1: " + str(a1) )

Print Int

This simple approach is recommended for printing commonly used data types.

To print int data type, Use string concatenation + operator and str() function. This is used to print int with some other string message.

# Example, print int
a1 = 10
print("Value of a1: " + str(a1) )

a1 = -100
print("Value of a1: " + str(a1) )

Print Float

This simple approach is recommended for printing commonly used data types.

To print float data type, Use string concatenation + operator and str() function. This is used to print float with some other string message.

# Example, print float
a1 = 10.0
print("Value of a1: " + str(a1) )

a1 = -100.005
print("Value of a1: " + str(a1) )

Print List, Tuple, Set elements on new line

This simple approach is recommended for printing commonly used data types.

If you are looking for printing list / tuple / set elements on separate line, below solutions are useful.

Solution-1: Use print function with sep="\n" (new line). It requires * - asterisk symbol - indicating unpacking of list / tuple / set.

Solution-2: Use .join() and generator. This is also useful to concatenate output with other messages.

# Example, print list / tuple / set elements on new line
#my_var = list( range(1,10) )
my_var = tuple( range(1,10) )
#my_var = set( range(1,10) )
print(my_var)

# Solution-1: Use print function and sep. Tried comma and new line together.
print(*my_var, sep=",\n")

# Solution-2: Use join function and list comprehension.
# Explained: for each element x of list my_var, str(x) are joined using ",\n" comma and new line
print(",\n".join( str(x) for x in my_var ) )

print("My message one: " + ", ".join( str(x) for x in my_var ) + ". And message two here.")

Print Dictionary key value on new line

This simple approach is recommended for printing commonly used data types.

If you are looking for printing dictionary elements on separate line, below solutions are useful.

# Example, print dictionary elements on new line
my_var = { 1: "Apple", 2: "Ball", 3: "Cat", 4 : "Door", 5: "Elephant"}
print(my_var)

# Solution-1: using .items()
for key,value in my_var.items():
    print("{} : {}".format(key,value) )

# Solution-2: using .keys()
for key in my_var.keys():
    print("{} : {}".format(key,my_var[key]) )

# Not in new line, but concatenated with other message.
print("My message one: " + ", ".join( "{} : {}".format(key, value) for key,value in my_var.items() ) + ". And message two here.")

In this page...

Copyright © All rights reserved.