Today, I was given an assignment. My assignment wanted me to write a Python program that pints the length of a string.

This is what I had to print out:

The length of "" is 0.
The length of "H" is 1.
The length of "Hello" is 5.
The length of "Amazing" is 7.

To do this, I have a plan in my mind which is:

Step 1. Find out what I’m going to use.

Step 2. Write it out.

Step 3. Finishing up.

1. Functions I’m going to use

I’ve got 3 functions I want to use. I’m going to use print, len, and f string. My reasons:

print so that I print out what I want to type.

len because I want to type the length of what I’m going to type.

f string so that I join the things together.

2. Writing it out

I’m going to start with the print funciton and then add the parenthesis. Inside it, I’ll add the f string at the front and then the single ’’ quotation marks instead of the double because what I want to print has double quotation mark. If I just do double quotation marks, the program doesn’t know which belongs to who, so I split up. In the single quotation mark, I’m going to write: The length of and then put the double quotation marks after and then write is.

So far this is how it looks like:

print(f'The length of "" is')

Reminder: I’m just going to do the first one.

After the is, I use the squiggly brackets because I’ve got the f string. Inside the squiggly brackets ({}), I’m got to use the function len to find out the length of "" and then after the squiggly brackets, I’m going to put a full stop to end the sentence.

Step 3. Finishing up.

So therefore, this is what it’ll print:

print(f'The length of "" is {len("")}.')

I do this for the whole thing but changing just a bit. So this is what I’ll print after:

Input:

print(f'The length of "" is {len("")}.')
print(f'The length of "H" is {len("H")}. ')
print(f'The length "Hello" is {len("Hello")}.')
print(f'The length "Amazing" is {len("Amazing")}.')

Output:

The length of "" is 0.
The length of "H" is 1.
The length "Hello" is 5.
The length "Amazing" is 7.

Another way to do this:

Oh wait, I still got free time. So now I’m just going to show you another way to do this. I’m going to use for loops and lists this time.

First I’m going to make a list called items and then put the "", “H” and stuff like that:

items = ["", "H", "Hello", "Amazing"]

Then I’m going to use for loops and do the direct way.

for i in items:

And then for the indented, I’m going to do the f string part.

Also I do the "" out of the {} because that’s what we want to print between.

So this would be how I print it out:

    print(f'The length of "{i}" is {len(i)}.')

So this would be the whole input:

items = ["", "H", "Hello", "Amazing"]


for i in items:
    print(f'The length of "{i}" is {len(i)}.')

Which will also give the output:

The length of "" is 0.
The length of "H" is 1.
The length "Hello" is 5.
The length "Amazing" is 7.

Thanks for reading.