Can the dog_years function utilize string formatting to produce the output?

Question

In this exercise, the instructions to create the string returned by the dog_years() function makes it appear like string formatting can be used. How would that be done?

Answer

Instead of using string concatenation and the str() function to produce the output, Python’s string formatting can be used. The following code example shows how to produce the desired output.

def dog_years(name, age):
    return "{name}, you are {dog_age} years old in dog years".format(name=name, dog_age=(age * 7))
7 Likes

def dog_years(name,age):
return f"{name}, you are {age*7} years old in dog years"

works as well

1 Like

What about this code?

Write your dog_years function here:

def dog_years(name,age):
dog_years = age * 7
print(name+", you are “+ str(dog_years)+
" years old in dog years”)
return age*7

#print(dog_years(“Lola”, 16))

should print “Lola, you are 112 years old in dog years”

dog_years(“Lola”, 16)

The code returns the expected value, so… it´s ok or not?

The idea of this lesson is to replace concatenation with string formatting…

>>> def dog_years(name,age):
    dog_years = age * 7
    print("{}, you are {} years old in dog years.".format(name, dog_years))
    return age * 7

>>> dog_years("Lola", 16)
Lola, you are 112 years old in dog years.
112
>>>

Can someone tell me where the .format concept was taught prior to this challenge? I do not remember that being covered up to that point.

The old course (Python 2) had a unit on string formatting, perhaps they omitted to include that unit in the Python 3 course? Can’t say, for sure.

Briefly, there are three ways of formatting strings:

  1. % (modulo) placeholder
  2. str.format() method
  3. f-strings

The latter may not yet be supported in the LE but the other two are.

a = 'fox'
b = 'dog'

s = "The quick brown %s jumped over the lazy %s" % (a, b)

t = "The quick brown {} jumped over the lazy {}".format(a, b)

Note that with string formatting we do away with concatenation when interpolating variables; and, all those pesky quote marks.

You’ll find everything you need in this article…

https://pyformat.info/

To learn about f-string formatting, check here…

https://www.python.org/dev/peps/pep-0498/

If we are talking about this exercise, it does not require, nor does the “solution” offer old- or new-style string formatting, just concatenation.

I don’t think that any of the Python courses discuss string formatting EDIT: My mistake - Thank you, @fox3773 , except for a bit on the %-style in Python 2. Someone please correct me if I’m wrong.

It’s well worth your while (and quite easy, the basic stuff, at least) to pick it up. Look here, for instance.

The Python 3 course does have 2 exercises covering the .format() string method in lesson 6: strings. I don’t think any other ways to format strings are mentioned, or at least they are never included in any of the exercises.

As @jjpeters1986 mentioned though it is definitely not something that has been covered yet at this point in the coarse.

Even so, I liked how these questions at the end of exercises sometimes touched on concepts or methods / functions, that the coarse had not yet directly covered. This one in particular I happened to find very useful as I absolutely hated concatenating strings together, and I also just think it looks ugly personally. So after seeing this post I was happily using the .format() method far before it is officially covered much later on in the coarse.

Thank you @patrickd314 @mtf @fox3773 for your responses. I’m just happy to know that I have a community of other people to help me out!

2 Likes

Hi kingkoalakingdom,
What does the “f” stand for?
I have simply written:

return name+", you are "+str(age*7)+" years old in dog years"
Thank you for the clarification!

Do a quick search for f-string, the newest form of string formatting in Python (since 3.6 or 3.7).

1 Like

Thank you very much!

1 Like

This is another way to write the same function
def dog_years(name, age):
return “{}, you are {} years old in dog years”.format(name, age*7 )

I tried many formats, all giving the “Lola, you are 112 years old in dog years” format.
Some examples:

def dog_years(name, age):
dog_years = age * 7
return “{}, you are {} years old in dog years”.format(name, dog_years)

OR

def dog_years(name, age):
return name + “, your are " + str(age*7) + " years old in dog years”

But here is the only one that the exercise took as valid:

def dog_years(name, age):
dog_years = age * 7
return name + “, you are " + str(dog_years) + " years old in dog years”

Hope it helps