Exercise - Build a program to say Hello

Now let's use what you learned about print, input, and string concatenation to display a personalized greeting using a Python script.

  1. Start the online code editor by typing code at the terminal prompt.

  2. Type or paste the following statements into the editor.

    print('Welcome!')
    name = input('Enter your name: ')
    print('Hello, ' + name)
    
  3. Save the file as hello.py. You can click the ellipse "..." in the right corner of the cloud editor, or use the accelerator key (Ctrl+S on Windows and Linux, Cmd+S on macOS).

  4. Close the editor through the "..." context menu.

  5. You can verify the file is on disk using the ls command.

  6. Execute the script using the Python interpreter.

    python hello.py
    
  7. When prompted, type your name and press Enter.

  8. Confirm that a personalized greeting appears in the output:

    Welcome!
    Enter your name: Hannah
    Hello, Hannah
    

If you want to add an exclamation mark after the name so the greeting reads "Hello, Hannah!", how would you do it?

Tip

A single Python statement can use multiple + operators to concatenate several strings.

Modify the program to add an exclamation point to the end of the output and run the app to verify the results.