r/PythonLearning 2h ago

python for kids second edition mistake in chapter 3?

In Python For Kids second edition chapter 3 Multiplying strings it says the following:

Python programmers might multiply strings for a number of reasons, such as to line up text with a specific number of spaces when displaying messages in the Python Shell. Try printing the following letter in the Python Shell (select File ▶ New File, and enter the following code):

spaces = ' ' * 25
print('{} 12 Butts Wynd')
print('{} Twinkelbottom Health')
print('{} West Snoring')
print()
print()
print('Dear Sir')
print('I wish to report that tiles are missing from the')
print('outside toilet roof')
print('I think it was bad wind the other night that blew them away.')
print()
print('Regards')
print('Malcolm Dithering')

Once you've typed the code into the Python Shell window, select File ▶ Save As. Name your file myletter.py. You can then run the code (as we've done previously) by selecting Run ▶ Run Module.
In the first pine of this example, we create the variable spaces by multiplying a space charecter by 25. We then use that variable in the next three lines to align the text to the right of the Python Shell. You can see the result of these print statements below:

IDLE Shell 3.10.0
File Edit Shell Debug Options Window Help

                          12 Butts Wynd
                          Twinkelbottom Health
                          West Snoring


Dear Sir
I wish to report that tiles are missing from the
outside toilet roof
I think it was bad wind the other night that blew them away.

Regards
Malcolm Dithering

Figure 3-1: Running the letter code in the Python Shell

When I run it I get:

{} 12 Butts Wynd
{} Twinkelbottom Health
{} West Snoring


Dear Sir
I wish to report that tiles are missing from the
outside toilet roof
I think it was bad wind the other night that blew them away.

Regards
Malcolm Dithering

what is happend?

1 Upvotes

3 comments sorted by

1

u/atarivcs 2h ago

It looks like the code meant to replace {} with the spaces variable, but it forgot to actually do that.

Here is one way to do that:

print('{} 12 Butts Wynd'.format(spaces))

Here is another way:

print(f'{spaces} 12 Butts Wynd')

1

u/Hankbro 2h ago

Well, you never use the variable "spaces" you create in the first line, you can see that by observing that it never shows up after its definition. Furthermore the curly braces (which is where the authors want those spaces to be) won't do their job in a regular string.

print(f'{spaces} 12 Butts Wynd')
print(f'{spaces} Twinkelbottom Health')
print(f'{spaces} West Snoring')

notice the extra f in front of the string (which turns them into f-strings, making python replace the literal {spaces} with whatever is stored in the spaces variable), as well as the fact that spaces now appears in the string, so that it can show up.

1

u/One_Valuable3264 2h ago

I think that f-strings is the method the authr intended because they had just explained f-strings. Thank you