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
4 answers

How to use a variable as dictionary key using f-string in python?

I'm trying to print dictionary item value of a dictionary but my item is also an another user input variable. How can I print the selected value with f-string? option = '' languages = {'1':'Learn Python', '2':'Learn Java', '3':'Learn C++',…
-1
votes
1 answer

How set a standard format specifier to be used throughout a module when using f-strings?

I am using f-strings for formatting strings and injecting variable values into the string, is there a way to set a format-spec for an entire module in python? I am aware of Standard Format Specifiers which can be used to specify formats for each…
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
-1
votes
1 answer

python f-string how to use the {} character for a variable and as a string

data = f'"{name}": ' + "{" + tmp + "}," I have such a string, but I need so that I can use the string {} as for a variable or just as a string. What do I need to do for this?
-1
votes
1 answer

Why does my basic print statement work, but my f-string version produces a syntax error?

Very early stages of learning Python, and trying to complete 100 days of code challenge. Am building a crude blackjack game, but am stuck on what I am sure is probably a very simple fix that I don't recognise! This is my code so far: import…
-1
votes
1 answer

ValueError: '=' alignment not allowed in string format specifier. Sometimes not working

Error message: alarm = f"{alarm_hour}:{alarm_minute:02}:{alarm_am_pm}" ValueError: '=' alignment not allowed in string format specifier I don't know why this code is giving the error as I have used the same before in the 's program and it's…
codework
  • 25
  • 1
  • 9
-1
votes
3 answers

Nested f-strings?

So I have something complicated but here is a simple version of it: i, j = 1, 2 a_1, a_2 = 1, 2 f"{a_f'{i}'} + {a_f'{j}'} = 3" == '1 + 2 = 3' I want an expression like in the left side to give a result like in the right side, the one I wrote here…
Matsukazi Issy
  • 109
  • 2
  • 8
-1
votes
1 answer

f-string separating new line

Does anyone know how to make this code into an f-string (if possible): print(">>> Multi-line Comment", data, sep="\n") So if there is 2 or more components in data it would print each in a new line. The above code works, its was just bugging me that…
-1
votes
1 answer

SyntaxError: EOL while scanning string literal while doing string interpolation for file names

I am trying to write the following line for a function: a.to_csv("~\Desktop\" + file.split('\\')[-1]) but I get SyntaxError: EOL while scanning string literal I also tried an f string: a.to_csv(f"~\Desktop\{file.split('\\')[-1]}") but I…
ACan
  • 105
  • 1
  • 1
  • 7
-1
votes
1 answer

How to trim spaces in an f-string?

I have a string I am formatting and printing using f-string and I need to eliminate some spaces. Here is my code: if len(probably) > 0 and len(might) > 0: print(f'{name}:',f'Might({might})', f'Probably({probably})') elif len(probably)…
Thesqlkid
  • 27
  • 6
-1
votes
1 answer

How to exclude the bracketed characters in a Python f-string using regex?

Recently, I have been creating an editor in Python 3.7.6 (using tkinter), I created the following syntax for highlighting single, double and triple quotes but I want to exclude all the characters inside a curly bracket of an f-string, I tried using…
prerakl123
  • 121
  • 1
  • 11
-1
votes
1 answer

Escaping brackets in strings python fstrings

I'm trying to substitute a variable called id in the below string query = '''SELECT ?item ?itemLabel WHERE {?item wdt:P279* wd:Q7930989. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }}''' I'd like to do something…
gatech-kid
  • 67
  • 8
-1
votes
1 answer

From string to fstring

I'm trying to pull a string from JSON, then convert it to an f string to be used dynamically. Example Assigned from JSON I get whose_fault= "{name} started this whole mess" How to build a lambda to convert it to an f-string and insert the given…
Starscream
  • 23
  • 9
-1
votes
2 answers

Why the code is stopping after first f-string?

I'm trying to print out two statements on the basis of provided dictionary, but something goes wrong and the code is stopping after the first f-string (the second isn't displayed). I suppose that it might be something similar, but can't figure out…
-1
votes
2 answers

Flask syntax f-string with single quotation mark

In a Flask tutorial, https://www.youtube.com/watch?v=cYWiDiIUxQc&list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH&index=4 There is a syntax I want to understand return f"Post('{self.title}', '{self.dateposted}')" Can someone decompose this line for me? f…
Dark Lei
  • 31
  • 1
  • 7
-1
votes
3 answers

How to iterate over a for loop with .format

I'm trying to insert my list values (test) into a variable (users). test = ['test', 'test1', 'test2', 'test3'] users = 'api.user_timeline(screen_name = {}, count = 10, wait_on_rate_limit = True)'.format(test) for user in users: …