Print hello world in python
Python is an easy to learn programming language, which was developed in 1980 by Guido Van Rossum as a successor to ABC Programming Language. According to the Stack Overflow Developer Survey 2022, Python is the 4th most used programming language in the world. This popularity of python is due to its “easy to learn” nature as well as its vast region of usage .
Python boasts of many features, some of which are:-
i) Easy to learn and begin with.
ii) It has automatic garbage collection so that we don’t need to worry about memory management.
iii) We can opt for either Structured programming approach or for Object Oriented Programming approach. Python supports them both.
iv) Python has a very large set of libraries which can be very helpful in creating various types of programs as well as other applications like Data Science, AIML, Web Development etc.
Python has its uses in almost every field of Computer Science now-a-days.
i) For AI and ML, we can use “keras” and “tensorflow” modules of Python.
ii) For Web Development, modules like “django” and “pyscript” are used.
iii) For Data Science, python provides very useful libraries like “matplotlib”, “numpy”, “pandas” etc.
iv) For Software Development, modules such as “tkinter” and “qt” are popular in python.
As we saw above, if anybody wants to learn programming in the current era, Python, being a very easy to learn language as well as extremely useful in almost every field, becomes a very obvious choice for the first programming language to begin with.
To print hello world in python, we can use two methods. We are going to cover both of these methods in our article.
Method 1:- Using print() function
We can print any string in python using a simple print function.
Let’s have a look at the code:
The print command can be used to print any numerical/string data in Python.
Below is a brief explanation about print() function:
print(object(s), sep=separator, end=end, file=file, flush=flush)
We can have a very fine control over our output using the above mentioned parameters if required.Here,
object(s) – The object you want to print. In python, everything (strings, numbers, dictionaries, lists etc.). We can print as many objects as we want.
sep – If we are printing more than one object, we can define how they are being separated. The default separation in python is space(‘ ‘). We can have separations like comma(,) , dot(.) , hyphen(-) , semicolon(;) , colon(:) etc.
end – It defines what we want to insert at the end of printed data. By default, python assumes a newline character (\n) at the end. But we can specify the ‘end’ part if we need to.
file – It specifies where we want to print the data. By default, it is sys.stdout
flush – It is a boolean used to clear the internal buffer of the data stream.
Method 2:- Using “sys” module
The “sys” module in Python provides us a way to interact with the Python interpreter at the core level. It gives us access to the functions and the variables which are system specific.
Let’s see an example of how we can use this module to print” Hello World “in python in Python
import sys # import the “sys” module
sys.stdout.write (“Hello World”) # use stdout.write() function to print the message
As we can see, in the above code, we first imported the “sys” module so that we can use the core functions to print our message.
After that we use the “stdout.write()” function to print our “Hello World” message. We need to write this message inside the brackets ().
You will be surprised to know that the “print()” function which we discussed earlier in Method 1, uses “sys.stdout” as the underlying code.
print hello world in python