-1

I am trying to work out how to complete a task that asks the following:

Create a string and use a method to generate a description of the new car as shown below. Print it – it should be formatted like the text below (including the line breaks).

Yesterday I bought a [Make] [Model].
  
It’s not “too” old. It was made in [year] …  

I like it, though. It’s [colour], and it has [doors] doors!

The variables (make, model, year, colour and doors) need to be populated from a dictionary.

I have programmed the dictionary to look like this:

Car = {
'Make': 'Mitsubishi',
'Model': 'Lancer',
'Year': '2002',
'Colour': 'Green',
'Doors': '4',
}

How can i fill the variables in that string of text by referencing the dictionary 'Car'?

Thank you!

Timus
  • 10,974
  • 5
  • 14
  • 28
dobby33
  • 13
  • 3
  • Your question has already been answered here: https://stackoverflow.com/questions/43488137/how-to-do-a-dictionary-format-with-f-string-in-python-3-6 – amy989 Mar 23 '22 at 11:25

3 Answers3

0

python uses f-strings for this. You can use f-strings to pass variables to your string, while still keep it readable.

output = f"Yesterday I bought a {Car['Make']} {Car['Model']}."

you use \n to represent a newline, and \ to escape any "" in the string itself:

output = f"Yesterday I bought a {Car['Make']} {Car['Model']}.\nIt’s not \“too\” old. It was made in {Car['Year']}"

extra: Pythons PEP8 style guides mention that variable names (like your dictionary) should always start with a lowercase.

Edit: Thanks to Timus for pointing out a small error. added '' around dict keys.

Damiaan
  • 777
  • 4
  • 11
  • This needs the dictionary to exist when the string is created. Which affects its reusability. So, maybe [`str.format()`](https://docs.python.org/3.10/library/stdtypes.html?#str.format) or even better [`str.format_map`](https://docs.python.org/3.10/library/stdtypes.html?highlight=format_map#str.format_map) might be a better idea? – Timus Mar 23 '22 at 11:57
  • Thank you for your timely response. I attempted that however I end up with an error stating that "Make' is not defined. The entire code is below. '''Car = { 'Make': 'Mitsubishi', 'Model': 'Lancer', 'Year': '2002', 'Colour': 'Green', } print(Car) Car['Year'] = '2011' Car['Colour'] = 'Blue' print(Car) NewCar = Car.copy() NewCar['Doors'] = '4' print (NewCar) print (Car) CarString = f"Yesterday I bought a {Car[Make]} {Car[Model]}.\nIt’s not \“too\” old. It was made in {Car[year]}" print(CarString)''' – dobby33 Mar 23 '22 at 12:00
  • I am very new to this so apologies for the formatting of my reply and code. – dobby33 Mar 23 '22 at 12:02
  • Sorry my bad! I forgot to add `""` around the keys in the dictionary. so `{car[Make]}` is now `{car["Make"]}` etc. – Damiaan Mar 23 '22 at 12:11
0

You can create a template string, and then burst the dictionary values to format it. Example.

fstr = "Yesterday I bought a {} {}.\n\nIt’s not “too” old. It was made in {} …\n\nI like it, though. It’s {}, and it has {} doors!"

Car = {
'Make': 'Mitsubishi',
'Model': 'Lancer',
'Year': '2002',
'Colour': 'Green',
'Doors': '4',
}
print(fstr.format(*Car.values()))

Gives an output like

Yesterday I bought a Mitsubishi Lancer.

It’s not “too” old. It was made in 2002 …

I like it, though. It’s Green, and it has 4 doors!

So, you can apply the format with any dictionary you want. Condition: You have to make sure the key/values are in the same order of the fillers.

Kris
  • 8,680
  • 4
  • 39
  • 67
0

Build a string with the dictionary keys as placeholders (included in curly braces), and then use .format_map() with the dictionary as argument on it:

string = '''Yesterday I bought a {Make} {Model}.
  
It’s not “too” old. It was made in {Year} …  

I like it, though. It’s {Colour}, and it has {Doors} doors!'''

Car = {
    'Make': 'Mitsubishi',
    'Model': 'Lancer',
    'Year': 2002,
    'Colour': 'Green',
    'Doors': 4,
}

print(string.format_map(Car))

(The ordering of the dictionary is irrelvant.)

Timus
  • 10,974
  • 5
  • 14
  • 28