In Python, we have discussed many concepts. In this tutorial, we will be discussing the role of the semicolon in python. The basic meaning of semicolon(;) in all the other languages is to end or discontinue the current statement.
In programming languages like c, c++, and java, we semicolon to terminate the line of code, and it is necessary to use. But in python, it is not compulsory to use a semicolon.
What are semicolon in python?
The semicolon in python is used to denote separation rather than termination—the use of it to write the multiple statements on the same line. If you write a semicolon, it makes it legal to put a semicolon at the end of a single statement. So. it is treated as two statements where the second statement is empty.
Role of the semicolon in python
- In python, we don’t use semicolons, but it is not restricted.
- Python does not use a semicolon to denote the end of the line.
- Python is a simple and easy coding language Because there we don’t need to use a semicolon, and if we even forget to place it, it doesn’t throw an error.
- The use of Semicolon in python is as the line terminator where it is used as a separator to separate multiple lines.
Examples to Use a Semicolon in Python
Here, we will be discussing how we can use the semicolon in python by different methods or ways:
1. Printing semicolon in python
In this example, we will normally be printing the semicolon inside the print statement. Let us look at the example for understanding the concept in detail.
#print semicolon print(";")
- In this example, we have directly applied the print function.
- Inside the print function, we have written a semicolon inside the Quotation mark so that semicolon gets treated as a string.
- Hence, you can see the output as the semicolon.
2. Splitting statements with the help of semicolons
In this example, we will first print the multiple statements in multiple lines with a print statement. After that, we will print all the statements in a single line separated by the semicolons between them. And see what happens with the help of semicolons in python. Let us look at the example for understanding the concept in detail.
print("Hy") print("Python") print("Pool") print("\n") print("Hy"); print("Python"); print("Pool")
Hy Python Pool Hy Python Pool
- Firstly, we have printed three statements with different print functions and in different lines.
- After that, we have applied a new line so that we understand the output more clearly.
- Then, we have applied all three print statements in a single line, separating them by the semicolon.
- At last, we have seen the output.
- Hence, you can see how you can write the print function but using a semicolon is not the correct order.
3. Using semicolons with loops
In this example, we will be using the semicolons with the for loop. Let us look at the example for understanding the concept in detail.
#using for loop for i in range (4): print ('Hi') ; print('Python Pool')
Hi Python Pool Hi Python Pool Hi Python Pool Hi Python Pool
- Firstly, we have applied for loop from i=0 to i=4.
- Then, we have put a semicolon and printed hi with the help of the print function.
- After that, again, we have applied a semicolon and printed the python pool.
- Hence, we have seen that we have written for loop in a single line, which worked properly.
How to print the semicolon in python
Here, we will be printing the semicolon inside the print statement with the help of some string. Let us look at the example for understanding the concept in detail.
print("Python pool is the best website ; ")
Python pool is the best website ;
Is using semicolon necessary in python?
Using a semicolon is not necessary, but it is also not restricted. It is only used as the line terminator, where it is used as a separator to separate multiple lines.
In this tutorial. We have learned about the concept of semicolons in python. We have also seen the role of it in python. After that, we have explained different examples of semicolons. All the ways are explained in detail with the help of examples. You can use any of the functions according to your choice and your requirement in the program.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.