How to use print() function in Python

How to Use Print Function | Data Types | Python

Introduction

Python is a versatile programming language that offers many built-in functions to developers. One such function is the print() function, which is used to display output on the screen. In this article, we will discuss the print() function in detail, including its use, the concept of single and double quotes in Python, the use of end with print(), and the concept of data types in Python.

The print() function is used to display output on the screen. It can be used to display text, variables, and expressions. Here is an example of how to use the print() function to display text:

print(“Hello, World!”)

In this example, the text “Hello, World!” is passed as an argument to the print() function, which displays it on the screen.

Examples of print() function in Python

ExampleCodeOutput
Print a stringprint("Hello, world!")Hello, world!
Print multiple stringsprint("Hello,", "world!")Hello, world!
Print a variablex = 10 print(x)10
Print variables with a stringx = 10 y = 20 print("The value of x is", x, "and the value of y is", y)The value of x is 10 and the value of y is 20
Print using escape charactersprint("Hello,\nworld!")Hello,<br>world!
Print using sep parameterprint("Hello", "world", sep=", ")Hello, world
Print using end parameterprint("Hello", end=" ") print("world!")Hello world!
Table of different ways to use the print() function in Python, along with their respective code and output.

Single and Double Quotes in Python

In Python, strings can be enclosed in either single or double quotes. There is no difference between using single or double quotes, and the choice is a matter of personal preference.

Here is an example of using double quotes to enclose a string:

print(“This is a string.”)

And here is an example of using single quotes to enclose a string:

print(‘This is also a string.’)

Both of these examples will produce the same output: “This is a string.”

Use of End with Print()

By default, the print() function inserts a newline character at the end of the output it displays. However, we can change this behavior by using the end parameter. This parameter allows us to specify what character we want to use instead of the newline character. Here is an example:

print(“Hello”, end=”, “)
print(“World”)

In this example, we have used the end parameter to specify that we want to use a comma instead of a newline character.

The output of this code will be: “Hello, World”

Data Types in Python

n Python, a variable’s data type is determined by the value that is assigned to it. Unlike some other programming languages, Python does not require the programmer to specify a data type when declaring a variable. Here is an example of declaring a variable in Python:

x = 42

In this example, the variable x is assigned the value 42. Python will automatically assign the data type of int to this variable because the value is an integer.

Common Data Types in Python

Data TypeExample CodeOutput
Integerx = 10<class 'int'>
Floaty = 3.14<class 'float'>
Stringz = "Hello, world!"<class 'str'>
Booleana = True b = False<class 'bool'> <class 'bool'>
Listmy_list = [1, 2, 3, 4, 5]<class 'list'>
Tuplemy_tuple = (1, 2, 3, 4, 5)<class 'tuple'>
Setmy_set = {1, 2, 3, 4, 5}<class 'set'>
Dictionarymy_dict = {"name": "John", "age": 30, "city": "New York"}<class 'dict'>
Overview of various data types in Python and their respective example code and output

Conclusion

The print() function is a basic but essential part of Python programming. By using this function, developers can display output on the screen and debug their code. Understanding the concepts of single and double quotes, end parameter, and data types in Python is important to write efficient and effective Python code.