Hello World Program in Python

Video Tutorial
FREE
Print Hello World thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

If you ask programmers, what was their first program? Most of them would say the "Hello, World" program.

It is a simple program that prints a Hello, World! message on the screen.

In this article, you will begin your journey into programming by writing the same hello world Program in Python.

Python Program to Print Hello world!

Printing Hello World using print() Function

To print "Hello, World!" using the print() function in Python, we can simply write the following code:

Code:

Output:

Explanation:

When we run this code, it will display "Hello, World!" in the console or output window. The print() function in Python is used to output text or values to the console. In this case, it will output the string "Hello, World!" as the specified text.

Printing Hello World using sys module

To print "Hello, World!" using the sys module in Python, we can use the sys.stdout.write() function.

Code:

Output:

Explanation:

In this code, we imported the sys module, which provides access to some variables and functions related to the Python interpreter. We use sys.stdout.write() to write the string "Hello, World!" to the standard output. The "\n" is added to print a newline character, which is equivalent to pressing the Enter key.

By using the sys module's stdout object, we can achieve the same result as using the print() function to display "Hello, World!" on the console.

Printing Hello World using String Variable

To print "Hello, World!" using a string variable in Python, we can assign the text to a variable and then use the print() function to display its value.

Code:

Output:

Explanation:

In the above code, we create a variable called message and assign the string "Hello, World!" to it. Then, we use the print() function to display the value of the message variable, which will output "Hello, World!" to the console or output window. Using variables allows us to store and manipulate strings or other types of data before printing them.

Printing Hello World using f-string

To print "Hello, World!" using an f-string in Python, we can use the following code:

Code:

Output:

Explanation:

In this code, we create a variable named message and assign it the value "Hello, World!". We then use an f-string by placing an 'f' before the string and enclosing the variable within curly braces {}. When the code is executed, the value of the message variable is inserted into the string, resulting in the output "Hello, World!" being printed to the console. F-strings provide a concise and readable way to include variables within string literals.

Conclusion

  • The simplest way to print "Hello, World!" in Python is by using the print() function. This function outputs text or values to the console.
  • Another approach is to utilize the sys.stdout.write() function from the sys module. It allows writing directly to the standard output.
  • You can store the "Hello, World!" text in a string variable and then use the print() function to display the value of the variable.
  • An f-string (formatted string literal) provides a concise way to include variables within a string. By using an f-string, you can insert the value of a variable directly into the string when printing.

See Also: