Skip to main content

Section 2.5 A Typical First Program

Traditionally, the first program written in a new language is called Hello, World! because all it does is display the words, Hello, World! In Python, the source code looks like this.
print("Hello, World!")
Some people judge the quality of a programming language by the simplicity of the Hello, World! program. By this standard, Python does about as well as possible.
This is an example of using the print function, which doesn’t actually print anything on paper. It displays a value on the screen. In this case, the result is the phrase:
Hello, World!
The quotation marks in the program mark the beginning and end of the value. They don’t appear in the result.
In the context of this book, we will use the print function to perform the output step in the algorithms we work with.
Here is the example in activecode. Give it a try!
The print("Hello, World!") statement in Python outputs the text "Hello, World!" to the screen. The quotation marks (" ") within the code define the start and end of a string value, which is a sequence of characters enclosed in quotes. The quotation marks tell Python that everything inside them should be treated as text, not code. When we run this code, Python displays only the text content between the quotation marks, so the quotes themselves aren’t part of the output. This is why the result displayed on the screen is simply: Hello, World!
In Python, and in most programming languages, a string is a data type used to represent text, such as words, sentences, or any sequence of characters. Strings are created by enclosing text within quotation marks, either single (’ ’) or double (" "). For example:
"Hello, World!"      # Using double quotes
'Alice'             # Using single quotes
Strings are versatile and widely used, from displaying messages to handling user input. Python allows flexibility in defining strings, so single or double quotes can be used interchangeably. This flexibility is especially helpful if your string contains an apostrophe or quotation marks, as in "It’s a beautiful day" or ’She said, "Hello!"’. Here is the example in activecode. Give it a try!
Check your understanding

Checkpoint 2.5.1.

The print function:
  • sends information to the printer to be printed on paper.
  • Within the Python programming language, the print function has nothing to do with the printer.
  • displays a value on the screen.
  • Yes, the print function is used to display the value of the thing being printed.
  • tells the computer to put the information in print, rather than cursive, format.
  • The format of the information is called its font and has nothing to do with the print function.
  • tells the computer to speak the information.
  • That would be a different function.

Checkpoint 2.5.2.

In Python, which of the following best describes a string?
  • A type of variable that stores only numeric values.
  • A sequence of characters enclosed in quotes.
  • A data structure used to store multiple items in an ordered way.
  • A type of function used to perform mathematical operations.
You have attempted of activities on this page.