-1

I understand what f strings are and how to use them, but I do not understand why we need to have apostrophes around the whole f string when we have already made the variables a string.

first_name = 'Chris'
last_name = 'Christie'

sentence = f'That is {first_name} {last_name}'

I understand what the expected result is going to be. But here's where I'm confused. Aren't the variables first name and last name already a string? So when we put it into the f string statement, aren't we putting two strings (the variables first name and last name) inside one big string (as the whole f string statement is surrounded by apostrophes)? Sorry if this is confusing

Granny Aching
  • 1,295
  • 12
  • 37
  • How would Python know where the f-string ended, otherwise? Keep in mind that a string can be part of an arbitrarily complicated expression. – jasonharper Apr 13 '19 at 23:12
  • 2
    The syntax is what the syntax is. There needs to be some way to tell where it begins and where it ends. There are other possibilities, but the designers of the language picked the one that they picked. I'm not sure what sort of answer you are expecting. – John Coleman Apr 13 '19 at 23:13
  • A complete explication can be found in https://cito.github.io/blog/f-strings/ – Mihai8 Apr 13 '19 at 23:19
  • One consideration is that it is a string. To *not* have a string terminated by a quote mark might violate the [principle of least astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment). – John Coleman Apr 13 '19 at 23:21
  • 1
    What is your proposed syntax? – Blorgbeard Apr 13 '19 at 23:24
  • If there were no single quotes how would the words "That is" get into the string? – John Kugelman Apr 13 '19 at 23:38
  • technically speaking, it could have be done not to require quotes. And there are other languages using different approaches, but as mentioned above the language designers made choices to reduce confusion. – Dr Phil Apr 14 '19 at 00:32

2 Answers2

1

aren't we putting two strings (the variables first name and last name) inside one big string

Yes. And in this example that's not a hard thing to do. As you suggest, you could just do:

first_name = 'Chris'
last_name = 'Christie'

sentence = 'That is ' + first_name + ' ' + last_name

to concatenate the strings. But this quickly gets unwieldy. Because of all those quotes and operators it's difficult to see at a glance exactly what the final string is going to look like.

So many languages have come up with a way of having the variables part of the string itself. That way you can write the string a bit more naturally. For example, in Python you can also use "%-formatting":

sentence = 'That is %s %s' % (first_name, last_name)

That's a bit better, because you can easily see where the variables are going to go in the string. But it gets messy when you have a lot of substitutions to do, because you need to match the order of the %'s with the order of the list. What if we could just put the variables in the string itself? Well, that's what f-strings do.

So f-strings allow you to see just where the variables are going to end up in the string. As you point out, the notation can look a little odd because you end up inserting strings as variables into a string, but notations are just that - notations. You get a bit of expressiveness at the cost of a bit of obscureness.

Heath Raftery
  • 3,643
  • 17
  • 34
1

Do not get confused about apostrophes:

  • We use apostrophes to define strings in Python:

    name = "Chris"
    
  • We use f-Strings as it is the new and improved way of formatting Strings in Python:

    # Define two strings
    name = "Chris"
    surname = "Christie"
    
    # Use f-Strings to format the overall sentence
    sentence = f"Hello, {name} {surname}"
    
    # Print the computed sentence 
    print(sentence)
    

    Output: 'Hello, Chris Christie'

Employee
  • 3,109
  • 5
  • 31
  • 50