F-strings in Class __repr__ Method


#1

https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-art-marketplace/modules/cspath-veneer/projects/veneer

I’m trying to set a class __repr__ method using f-strings. I know this isn’t necessary, and know how to do this using %-formatting or str.format() - I’ve not used f-strings before and wanted to try them out.

class Art:
  def __init__(self, artist, title, medium, year):
    self.artist = artist
    self.title = title
    self.medium = medium
    self.year = year
    
  def __repr__(self):
    return f'{self.artist}. "{self.title}". {self.year}, {self.medium}.'
  
class Marketplace:
  def __init__(self, listings):
    self.listings = []
    

 #Artworks

girl_with_mandolin = Art('Picasso, Pablo', 'Girl with a Mandolin (Fanny Tellier)', 'oil on canvas', 1910) 

print(girl_with_mandolin)

The above is returning;

File "script.py", line 9
    return f'{self.artist}. "{self.title}". {self.year}, {self.medium}.'
                                                                       ^
SyntaxError: invalid syntax

Could somebody please point out where I’m going wrong here?


#2

Current version of this environment,

3.5.2 (default, Nov 23 2017, 16:37:01)

f-string formatting isn’t introduced until 3.6.


#3

As always, thank you for the help - I now know how to check that if I need to.

import sys

print(sys.version)