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
vote
2 answers

How to stop pydoc from erroring out on f-strings?

I am using pydoc to automatically build documentation. My code is written for Python 3.6. It errors out with the following error: : invalid syntax. It happens whenever I use an f-string. Can you help me figure out…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
1
vote
1 answer

Python, f-string formatting float value to exact amount decimals

So I'm trying to learn Python, and decided to try and do some problems on Kattis, this one to be more precise. I've managed to scrape together some code that prints the correct value the case they provide. import functools for _ in…
1
vote
2 answers

Constructing a long Python string using variables

I have a python string which is basically a concatenation of 3 variables.I am using f-strings to make it a string. It looks like this now: my_string = f'{getattr(RequestMethodVerbMapping, self.request_method).value}…
Amistad
  • 7,100
  • 13
  • 48
  • 75
1
vote
2 answers

How to use .loc in fstrings?

I have a dataframe like this: import pandas as pd df = pd.DataFrame({'col1': ['abc', 'def', 'tre'], 'col2': ['foo', 'bar', 'stuff']}) col1 col2 0 abc foo 1 def bar 2 tre stuff and a dictionary like this: d =…
Cleb
  • 25,102
  • 20
  • 116
  • 151
1
vote
2 answers

How to clean packages compatible Python 2.7 + 3 to make them only Python 3.6 compatible?

For some projects, I'd like to stop supporting Python 2.7 (see http://python3statement.org/) to use only Python > 3.6 (or rather 3.5 + f-string, like Pypy v6.0). A first step could be to modify the setup.py file to get an explicit error if one try…
paugier
  • 1,838
  • 2
  • 20
  • 39
1
vote
3 answers

Are f-strings a unique feature to python (3.6 and higher)?

With an f-string you can fill in 'placeholders' in a string directly with some variable value. Opposed to, let's say using the str.format() method, often seen. Example: In [1]: x=3 In [2]: y=4 In [3]: print(f'The product of {x} and {y} is…
muuh
  • 1,013
  • 12
  • 24
1
vote
1 answer

Syntax errors with parsing f-strings in PyDev Eclipse

I've a python project using PyDev in Eclipse. For sample code like below, var = 'element' width = 11 print(f'{var:>{width}}') the code is executed printing the desired output element with right alignment and no errors. However, PyDev parses this…
Viswanath
  • 1,003
  • 2
  • 11
  • 21
1
vote
1 answer

Python3 f-strings only interpreted in bash

I am currently learning python and have noticed an issue: If I write a .py file and interpret it with the python3 myfile.py command line then the f-string below simply won't show, but if I do the same directly in the python bash it works just fine.…
Ugohihi
  • 37
  • 11
1
vote
1 answer

Should I use f-string when writing with eval()

So recently I learned that eval() is a surprising function that can turn a string into a code command, it could be quite useful when writing a function where an argument is a string of a function's name. But I wonder what is more pythonic way of…
jxie0755
  • 1,682
  • 1
  • 16
  • 35
1
vote
2 answers

Fix this code with f-string for me?

while I read this that has this code We can also do that this way: We'd have 500000 beans, 500 jars, and 5 crates. when I revised to pep 498 print ("We can also do that this way:") print (f"We'd have {secret_formula(start_point)} beans,…
kunjun
  • 11
  • 1
1
vote
1 answer

Query format with f-string

I have very big dictionary that I want to insert into MySQL table. The dictionary keys are the column names in the table. I'm constructing my query like this as of now: bigd = {'k1':'v1', 'k2':10} cols = str(bigd.keys()).strip('[]') vals =…
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
0
votes
1 answer

Python expressions in f-strings do not return same result as str() conversion for subclasses?

For example, we override the __str__ method in Decimal then call it two different ways from decimal import Decimal class D(Decimal): def __str__(self): return super().normalize().__str__() num =…
goweon
  • 1,111
  • 10
  • 19
0
votes
2 answers

Escaping Double Quotations in Python F-String Literal

When attempting to escape double quotation marks in a f-string literal in Python, I encounter a strange phenomenon where subsequent single quotes are automatically escaped. This is not what I want, as I intend to pass the "query" string as a SQL…
0
votes
1 answer

Generate Markdown using F-string with padding and formatting (value could be 'None' as well..)

I have variables called "variable" "size" and "unit. The values can be as following: variable = "short name = " or variable2 = "extra long long name = " and dimension = 1.2345 or dimension2 = 6.78 or dimension3 = None and unit = " mm" unit2 =…
0
votes
1 answer

Python SyntaxError on fstring in the __repr__ method of the class

My code: class Book: all = [] def __init__(self, ISBN, title, subject, publisher, language, number_of_pages): self.__ISBN = ISBN self.__title = title self.__subject = subject self.__publisher = publisher …
Raj
  • 3,637
  • 8
  • 29
  • 52