0

I'm using this tutorial to learn about lambda functions:

How to Use Python lambda Functions

There is an example involving this line:

full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'

I have 2 questions:

  1. There is an "f" in front of "Full name". What does this "f" do?

  2. When I run this line, I immediately get this error:

    File "<stdin>", line 1
        full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'
                                                                                   ^
    SyntaxError: invalid syntax
    

Why does this happen? Why did the tutorial show a properly executed function, but I get an error?

martineau
  • 119,623
  • 25
  • 170
  • 301
Iterator516
  • 187
  • 1
  • 11
  • "f-strings" are explained in the [Formatted string literals](https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals) section of the online documentation. – martineau Oct 05 '19 at 21:59

1 Answers1

0

So firstly this f is a new, elegant way to format a string with variables. I invite you to read https://realpython.com/python-f-strings/ that tells the whole story.

However, this exists only since Python 3.6. Can you confirm your version?

python --version

Kind regards -

Cedric Druck
  • 1,032
  • 7
  • 20