Questions tagged [f-string]

Literal string interpolation in Python

An f-string is the abbreviated name for a "formatted string" syntax that was introduced in Python 3.6 and extended in Python 3.7. They enable literal string interpolation:

>>> value = 80
>>> f'The value is {value}.'
'The value is 80.'

See PEP 498 for a comprehensive description of the feature, and also PEP 536 for the final grammar.

621 questions
-1
votes
1 answer

How to write a GPT prompt programmatically?

I have a template prompt similar to this one: prompt = f""" Write a poem about the topic delimited by triple backticks if it starts with a consonant, otherwise say "foo". Topic: ```{topic}``` """ and a list of topics: topics = ['cuddly pandas',…
DeltaIV
  • 4,773
  • 12
  • 39
  • 86
-1
votes
1 answer

Performance of f-string vs string.join

one = 'one' two = 'two' three = 'three' Are the 2 equivalent in time complexity? result_1 = f'{one}{two}{three}' result_2 = ''.join([one, two, three]) I know that join works in linear time. But is the same true also for f-strings? With…
okokok
  • 11
-1
votes
1 answer

Looping through directories in Python using f-string

I am trying to access 2 different directories in ADLS using for loop & f-string but facing issues in doing so # Primary storage info account_name = "accountnamehere" # fill in your primary account name container_name = "logscontainer" # fill in…
Lopa
  • 51
  • 6
-1
votes
1 answer

How to round inside a list using f string

I want to round some numbers inside of a list by using f-string, but it returns unsupported format string passed to list.__format__ This is my code IS_gradebooks = [['ISOM2020', [59, 100, 80, 55, 95, 87, 95, 98, 74, 69, 92, 94, 75, 97, 43, 57]], …
Gartz man
  • 3
  • 1
-1
votes
1 answer

In line 12,13,14 how can i write code by using f string, without using spaces in f string but in output there must be space between string

code that I am writing I tried to remove space but it is not giving me the desired output.output that I want without using space in f string
Dany
  • 1
  • 1
-1
votes
1 answer

Python F string, insert string with curly brackets inside curly bracket

I have a list of values I want to insert inside the parenthesis in an f-string. The length of the list will be different from time to time, therefore, I will make a string with the correct length for each case. The idea was to create a string of…
Tor O
  • 51
  • 8
-1
votes
1 answer

F-String in mail python

when I use f-string to print supplier names, I use this code: for i in range(1,10): print(f'{df_supplier_number["SUPPLIER NAME"][i]}') The result looks like this: supplier 1 supplier 2 supplier 3 supplier 4 supplier 5 ... But when using this…
Michael
  • 59
  • 8
-1
votes
1 answer

How can I do a dictionary format with f-string in Python

my code is as below aaa = "['https://google.com', 'https://yahoo.com']" REQUEST = f'{"siteUrls": {aaa}}' print(REQUEST) I've tried aaa = "['https://google.com', 'https://yahoo.com']" REQUEST = {"siteUrls": {}}.format(aaa) print(REQUEST) and aaa =…
Jeremy
  • 19
  • 4
-1
votes
1 answer

How can I debug this Python syntax error with f string

def write_users_group(heading_writer, heading, user_writer, users): heading_writer.writerow([f"{heading}", f"{len(users)} users"]) user_writer.writeheader() for user in users: user_writer.writerow(user) …
-1
votes
1 answer

Python f-string not formatted correctly for dict with variables

I have a payload for an API call, like so: start_period = "2022-05-02" end_period = "2022-05-02" payload = {"period": f"{{'p1': [{{'type': 'D', 'start': '{start_period}', 'end': '{end_period}'}}]}}", "max-results": 50, …
pymat
  • 1,090
  • 1
  • 23
  • 45
-1
votes
1 answer

Combining Python f-string and r-string not behaving as expected

I have a variable that contains a string, that I'd like to print literally, so the newline is escaped. Printing the variable does not escape the newline: bad_string = 'http://twitter.com/blo\r\n+omberg' print(bad_string) >>>…
jammygrams
  • 358
  • 1
  • 2
  • 9
-1
votes
1 answer

Trouble printing this f-string

I'm just trying this normal piece of code that has an f-string, but in the particular line mentioned below, I couldn't determine what's the problem: elif(BMI<25): print(f"Your BMI is {BMI),you have normal weight.")
-1
votes
2 answers

How to include keywords within fstring in python3

I am new to python and fstring, Can you please help me how to write multiple lines to the file with fstring, it is to create a config file with all the contents within the fstring is to be written to a file, ignoring the content what it has def…
Kiran ND
  • 327
  • 2
  • 4
-1
votes
1 answer

What does {foo:>7f} do in Python f-strings?

In particular, this is over my head: print(f"foo: {foo:>7f}, bar: {bar:>5d}") I can imagine that f indicates float and d indicates integer but I don't really understand what the >7f and >5d do. Note that I understand what print(f"foo: {foo}, bar:…
Essam
  • 395
  • 3
  • 15
-1
votes
2 answers

Pytest checking messages returned by errors

Something about the interaction between pytest, str() and python Error types breaks full error testing where we want to confirm the EXACT TEXT returned by the error. Example below: def erroring_func(name, required_item_list): # skip boring bit.…
Mark_Anderson
  • 1,229
  • 1
  • 12
  • 34