Program 1: Print Hello World! - 1000+ Python Programs


Question 1)

Write a program in Python to print "Hello World!" as an output on the console!


Code:
1-hello-world-program-python
hello-world.py
# Program 1
# Hello World!
# 1000+ Python Programs by Code2care.org

print("Hello World!")

Output:

Hello World!



Explanation:

This is the first program one tries when learning any new programming language. This is like a small test program to see that everything is good to get started.

⭐ We learnt the first function in Python 3.x called the print() function.

print() is a build in function in Python3

Syntax:
print(*objects, sep=' ', end='\n', file=None, flush=False)

Reference: https://docs.python.org/3/library/functions.html#print



Troubleshooting & most common errors:

Note: If you are using Python 2.x print is a statement and you will write the Hello World program like below,

print "Hello World!"

If you run the above program in Python3 you will get an error,

print 'Hello World!'
          ^
SyntaxError: Missing parentheses in call to 'print'. 
Did you mean print('Hello World!')?

Python3 also known as Python 3.0, Python 3000, or Py3K was released in the year 2008, which is not backward compatible with Python 2.x



If you miss the double or single quotes around "Hello World!" string.
# python3 hello-world.py 
File "hello-world.py", line 1
print("Hello World!)
                  ^
SyntaxError: EOL while scanning string literal


If you add spaces or tabs before the print function:
# python3 hello-world.py 
File "hello-world.py", line 1
    print('Hello World!')
    ^
IndentationError: unexpected indent


If you miss the closing parenthesis:
# python3 hello-world.py 
File "hello-world.py", line 2
    
                        ^
SyntaxError: invalid syntax

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap