Greeting App

Introduction

If you have read all the previous lessons, you should have encountered our basics of setting up a python project from scratch. You can view it to review if you want at Setting up a python project.

For this project though, we don't need to create a virtual environment. We just need to have a clean installation of Python 3 and we are good to go since we are gonna be installing any third party libraries.

This lesson is the first part of the app-based learning of the basics of python. In this series, we are going to build a series of apps to get you going in learning the basics of programming with Python.

What we will learn?

In this lesson, we will build a greeting app. The app will collect the name of the user and it will display a greeting with name specified in it.

After completing this, the student will learn the following:

  • Starting a python project from scratch
  • Displaying a message to the screen
  • Storing data to a variable
  • Using the value of the variable
  • Concatenation of strings
  • Interpolation of strings and other data
  • Getting input from the user.

Project setup

Now let's setup our project. Create an empty directory. For me I will call it greeting.

Inside the directory, create a new file called main.py. You can name it whatever you want, but for me I will use this.

Displaying a message to the screen

To display some texts to the screen, we will use the function print() in python.

Let's try this:

print('Hello, World!')

Type it inside the main.py file and save it. Now assuming that you have a successful installation of python interpreter in your computer, let's run the program by typing in the terminal:

python main.py

or

python3 main.py

I also assume that you have VS code installed. Inside VS code, you can open the terminal under Terminal > New Terminal.

You should have the following on your screen.

VS Code preview with terminal
VS Code preview with terminal

We can now type in the command at the bottom (which is our terminal inside VS Code).

For my setup, I am using python3 as the command because I have another version of python installed.

Running the program
Running the program

As you can see above, the message "Hello, World" is displayed after hitting ENTER on our command for running our python script.

Storing data to a variable

For this part, we will use what is called in programming a variable. A variable in python can hold any type of data. The first data type we will look is a string. A string is a set of texts data. To create a variable with a string value, we will do this.

name = 'Alex'

print('Hello, World!')
 


As highlighted in the code above, we just set the name of the variable, in this case, I called it name but you can call it whatever you want with some exeptions, and then put in the equal sign (=) after and then name value we want.

In this case, I want the variable name to have a value of "Alex".

Different ways to initialize a string in python

There are two ways to set a value as string in python.

  1. First is what we used, by using a pair of single quotes surrounding the value (e.g. 'Alex'),
  2. And the second one, we can use double quotes. (e.g. "Alex")

We can use any of them.

Using the value of a variable

Now that we have a variable with a value, we will try to use it using the print function.

Let's replace the code in line 3 with the following:

name = 'Alex'

print(name)


 

Now if we run our app, the output will be like the following,

Preview
Preview

Concatenation of strings

Now that we have a variable and we know how to print it, our app now prints tha value or the name. However, if you can see, it's a little bit boring right? It just spits out the name. And so what?

We will now improve our program to incorporate the value of our variable in our greeting.

Wouldn't it be better if our program will say something like:

"Hello, Alex! Welcome to my greeting app!".

In python, we can use the concept called "concatenation" or the combining of two or more strings. To do this in our app, we can disect our proposed greeting. In python, we use the '+' sign to add two or more strings.

For example, if we have a string "Hello" and another "World", we can combine them like

print("Hello" + "World")

Now you may guess the output of this. It will output "HelloWorld". No spaces.

To add some space, we have to do it ourselves like so,

print("Hello" + " " + "World")

This will put a space in between the two words.

Now I hope you get the idea. Let's try it in our program.

name = 'Alex'

print("Hello, " + name + "! Welcome to my greeting app!")


 

Let's try to run this now.

Preview
Preview

Interpolation of strings

There is another and more elegant way that we can achieve the same result as above, and that by using f string in python or in some language, an interpolation.

This is introduced in Python3.6 and goes like this.

name = 'Alex'

print(f"Hello, {name}! Welcome to my greeting app!")


 

As you may have noticed, this is done by placing the letter f before the string quotation marks. And then we can surround any variable inside the quotations marks with the pair of curly braces like we did above with the name variable.

Getting input from the user

Now, we cannot say that our program is dynamic. It doesn't allow input from a user. So what we will do now is we will get the name as input from the user and then do everything else as above. The only code we will change is the initialization of the variable name.

To do this, we will use the function input() in python.

Inside the input parenthesis, we can put in the message that the user will see before they enter a value.

name = input("Enter you name: ")

print(f"Hello, {name}! Welcome to my greeting app!")

Here, we are getting an input from the user and any data he/she types, will be saved in the name variable. Now if we run this and type in "Alex" as the name:

Preview
Preview

Conclusion

I hope you coded along with the lesson. This will definitely increase you learning better than just reading through it. We have learned some basic programming concepts with this simple app.

We learned how to get input from a user, put it in a variable and use it to display some message in the screen.

For any question about this lesson, please feel free to post it down below.

Last Updated:
Contributors: alexiusacademia