What is (f) in (f"""), and what is (\"\"\") in (\"\"\"{text_1}\"\"\")

Hi! I am really enjoying this course. Thanks!

I am wondering if someone could tell me more about what (f) is doing in (f"“”) before our delimiters, and what is ( \ " \ " \ " ) in (\ " \ " \ "{text_1} \ " \ " \ " ) ?

They seem set up very specifically. In the example below the ( " \ " \ " ) seems set up different than in other prompts. I understand the concept of using delimiters in relation to the prompts, but those two things have me slights confused as they seem like ‘extra’. Thank you!

text_1 = f"""
Making a cup of tea is easy! First, you need to get some \ 
water boiling. While that's happening, \ 
grab a cup and put a tea bag in it. Once the water is \ 
hot enough, just pour it over the tea bag. \ 
Let it sit for a bit so the tea can steep. After a \ 
few minutes, take out the tea bag. If you \ 
like, you can add some sugar or milk to taste. \ 
And that's it! You've got yourself a delicious \ 
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes. 
If it contains a sequence of instructions, \ 
re-write those instructions in the following format:

Step 1 - ...
Step 2 - …
…
Step N - …

If the text does not contain a sequence of instructions, \ 
then simply write \"No steps provided.\"

\"\"\"{text_1}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)

Hi @MyTH.
Very briefly:

  1. “f” here means “formatted string literals”, so if you use it before any string part in courly bracket is replaced by a value, formula, etc.
  2. if you use tripple quotes it denotes multiline text. To be able to read it a convenient way you use ‘\’ to continue with text on another line (It’s an escape character).

Hope it helps a little bit.

1 Like

@Honza_Zbirovsky

AHH yes, I remember the “f” now. Thank you. I am still learning python :slight_smile:

As far as the second part. I was more curious as to why \ " \ " \ “{text_1} \ " \ " \ " was used (with the slashes) in this example, as opposed to “””{text}“”"
(Without slashes) in other examples. Thanks!
Like here:

text_1 = f"""
You should express what you want a model to do by \ 
providing instructions that are as clear and \ 
specific as you can possibly make them. \ 
This will guide the model towards the desired output, \ 
and reduce the chances of receiving irrelevant \ 
or incorrect responses. Don't confuse writing a \ 
clear prompt with writing a short prompt. \ 
In many cases, longer prompts provide more clarity \ 
and context for the model, which can lead to \ 
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \ 
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)

Isa said, as far as I can remember, it doesn’t matter. Just to have something strictly separate.

1 Like

I asked ChatGPT (I guess I could have done that to begin with lol):

In this example, the {text_1} expression within the prompt variable is indeed surrounded by triple quotes and backslashes. This is done to include the entire text_1 variable as a part of the prompt string while maintaining the line breaks and formatting of the original text_1.

The backslashes at the end of each line inside text_1 are for line continuation, as previously explained. When it comes to the prompt variable, the backslashes at the end of the lines still serve the same purpose of line continuation. However, when it comes to the lines that contain """{text_1}""", the backslashes before the triple quotes are escape characters. They tell Python to treat the triple quotes as part of the string, rather than the delimiter that marks the beginning or end of the string.

So, in this example, the combination of backslashes and triple quotes around {text_1} is used to maintain the formatting and line breaks of text_1 and to include the triple quotes as part of the prompt string.

So basically, just formatting. Thanks for your help.