100 Days of Python - Day 1: String Printer
This is day 1 of 100! Let’s get the ball rolling with a simple string printer. You will learn the following in this project:
- variables
- data types
- lists
- iterating lists
- for loops
- the
print()function
I hope you enjoy the read! You can find the video on here on my YouTube channel if you are more keen on video tutorials.
🐍 Ensure Python is installed
Python can be downloaded from the Python.org Downloads page. If you have issues with that link, what you will want to do is head over to Python.org and click on the Downloads tab in the navigation bar.
Here you can download different versions of Python. Unless you are looking for something specific, the best option is to grab the latest stable release so you can make use of Python’s latest and greatest features. Once you download the installer for your system (Windows, Mac, or Linux), go ahead and run the installer.
For more indepth installation information, check out my post on how to install python.
🧵 Strings? Variables? Loops?
Before we begin, you should understand a few different aspects of programming. Normally, when you follow along with a basic course you learn the smallest building blocks first. However, I am taking a different approach with this one. I find it much more interesting to learn about things when you can actually see them in action. That’s why I want to go over a few basic principles that we will get into during this project.
First of all, variables are a core principle in any programming language. Essentially, a variable is a storage device for a value. That might sound weird, but in the most basic terms we are giving a name to a value. That way we can just call the name throughout the application and only have to change it in one place. With that one change it will update the value of everything that calls the variable name going forward.
Example:
myString = "Hello!"
In the above example, we assign the value "Hello!" to variable myString. You can assign all sorts of data to them, which is what we will go over now.
Data types are… well the different types of data. We have plenty to go over, but I will mainly focus on one in this project:
- Strings: Words, letters, values that are meant to be read as a language, not a number or anything else. In the above example, we used
"Hello!"which is a string since it is notated with quotes. - Numbers: There are a few different number types: integers (whole numbers), floats (decimal numbers), and so on. You don’t have to declare the variable type in Python, it will automatically hand that for you. You declare it the same as any other variable, except you will not use quotes. Only numbers work for this:
myNumber = 12.3. Python knows automatically that this is a float and handles assigning it tomyNumber. - Boolean: This is your basic
TrueandFalsevariable. That is the only two values a boolean can be.
There are more, however I just wanted to touch on them for the sake of this project.
Next, we have lists. Lists are essentially arrays of values. You can assign it to a variable like anything else, and that is what we will be doing in this project. These values can be any data type, the values are separated by commas, and it is notated by square brackets.
Example:
myList = [
"String One",
"String Two",
"String Three
]
As you can see above, it is a list of strings in this instance separated by commas.
Now, loops are a super power that all developers need to master. You will use these for all sorts of things, however I won’t go into great detail since this is only Day 1. Loops are used to loop over and over again until a value is met. This makes it fantastic for iterating (reading) over lists.
In this project, we will use the for loop.
Example:
for person in team:
# code goes here
In the above example, for every person in the team list, we will write code that does something. It will go through and execute the code on every value in the team list. Something to note, person is a variable here. Every value that we come across will be assigned to the person variable so we can easily do things with it inside of the for loop.
Last, but not least, the print() function. Functions are reusable code blocks that allow us to call a name, much like a variable, and run the code in it without us having to type it out over and over. The print() function comes standard in Python. It’s role? To print things to the console 😁.
Example:
print("Hey, there!")
In the above example, we print Hey, there! in the console. It accepts different data types, but we will be working with strings in this project. Now, on to the fun part… coding!
🧵 Create the String Printer
First off, create your file somewhere that you can access. I named mine stringPrinter.py. Once that is done, open it in your favorite code editor.
Let’s start simple:
stringOne = "Hello, World!"
print(stringOne)
Run your application in the console, and you should see the following:
Hello, World!
That’s all fine and dandy, but how about printing more strings?
stringOne = "Hello, World!"
stringTwo = "Hello, Again!"
stringThree = "Hello, Finally!"
print(stringOne)
print(stringTwo)
print(stringThree)
When you run that, you will see that it did indeed print the three strings:
Hello, World!
Hello, Again!
Hello, Finally!
We could stop there, since you learned how to print those strings… but that is a lot of work to keep adding the print() function for every string, right after having to create a whole new variable for each string. Let’s go deeper!
Comment all of that out (use # to make a comment). Now you should see the following in your code:
# Static way of printing strings:
# stringOne = "Hello, World!"
# stringTwo = "Hello, Again!"
# stringThree = "Hello, Finally!"
# print(stringOne)
# print(stringTwo)
# print(stringThree)
I labeled it the static way in my comment. Now since these are commented, these commands will not run. Let’s create a list of strings! Ill name mine strings:
strings = [
"Hello, World!",
"Hello, Again!",
"Hello, Finally!",
"Another string!",
"Even another string!",
"Day 1 of 100... Complete!"
]
That’s a whole lot of strings right? Notice that we didn’t have to create a new variable for each one here. We just assigned it to the list. The next part is where the magic happens… the for loop!
for i in strings:
print(i)
Before you run the code, look at how we wrote that loop. for i in strings assigns each iteration value to the variable i. So for each value, this for loop will execute print(i) and print each one to the screen dynamically for us.
When you run your application, you should get the following:
Hello, World!
Hello, Again!
Hello, Finally!
Another string!
Even another string!
"Day 1 of 100... Complete!
🎆 Congratulations! Your first day of Python is done. You learned quite a bit, and if you aren’t new you still practiced which as we know practice makes perfect.
You can try recreating this project with new strings, or try to create it without looking at your notes or the article. This way, you really can see how much you are understanding and what might need work.
The complete code:
#################################################################
#################################################################
# Static way of printing strings:
# stringOne = "Hello, World!"
# stringTwo = "Hello, Again!"
# stringThree = "Hello, Finally!"
# print(stringOne)
# print(stringTwo)
# print(stringThree)
#################################################################
#################################################################
# Dynamic way to print by iterating through a list
strings = [
"Hello, World!",
"Hello, Again!",
"Hello, Finally!",
"Another string!",
"Even another string!",
"Day 1 of 100... Complete!"
]
# for loop to print the contents of each list item
for i in strings:
print(i)
💻 Where to Find the Code
You can find the code for this project here on my GitHub page!
Good luck on your development journey, thanks for letting me be a part of it!