687 questions
0
votes
0
answers
47
views
Whitespace after '{' for set comprehension in f-strings [duplicate]
What is the correct way to define set/dictionary comprehension in f-strings, so that flake8 does not complain? Currently I get the following errors:
echo 'f"{ {k for k in []} }"' | flake8 -
...
0
votes
1
answer
224
views
Why doesn’t this f-string with double quotes raise a SyntaxError in Python 3.13.3?
I'm currently following this Python course course from Harvard.
At the point in the video, the instructor shows that the following code should raise a SyntaxError because of the conflicting double ...
3
votes
2
answers
208
views
Make subclass use custom `__str__` in an f-string
I wrote a subclass of Decimal to represent an amount of money. I wrote a custom __str__ to display the currency along with a sign format. My method works when calling str() but in a f-string somehow ...
1
vote
2
answers
93
views
regex to match uuid4 not working with f-string [duplicate]
Working:
#!/usr/bin/python3
import re
path = "/a/b/c/e72cc82c-e83a-431c-9f63-c8d80eec9307"
if re.match(r"/a/b/c/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$&...
2
votes
1
answer
111
views
why is transforming a string to octal value \ooo isnt working when i use an f-string and a variable inside {}?
When using a backslash followed by three integers, Python interprets them as an octal value:
# A backslash followed by three integers will result in an octal value:
txt = "\110\145\154\154\157&...
3
votes
2
answers
81
views
How to reduce verbosity of self-documenting expressions in Python f-strings?
This script:
import numpy as np
a = np.array([2, 3, 1, 9], dtype='i4')
print(a)
print(f'{a=}')
produces:
[2 3 1 9]
a=array([2, 3, 1, 9], dtype=int32)
Is there a way to get just a=[2 3 1 9] from {a=}...
2
votes
1
answer
187
views
Convert numpy float to string
import numpy as np
number = np.float32(0.12345678)
assert str(number) == "0.12345678"
assert f"{number}" == "0.12345678359270096"
Why is this different when converting ...
1
vote
1
answer
70
views
Snakemake expand a string saved in a variable
My question is very simple, but I can't find how to do it in the Snakemake documentation.
Let's say I have a very long string to expand, like :
rule all:
input:
expand("sim_files/test_nGen{ngen}...
3
votes
3
answers
167
views
Combine Python f-string with printf style formatting?
I need to format a command line where some parameters come from simple variables, and others are the result of a longer expression. Python f-strings work well for the variables, and I'm using a printf-...
0
votes
4
answers
212
views
f string vs .format in manipulating a file name
I am trying to manipulate a file name to remove an underscore.
final_name = '{0}{1}{2}_{4}_new'.format(*filename.split('_'))
The filename is something like - 2024_10_10_091530_xyz_new.txt and the ...
0
votes
2
answers
438
views
Pycharm keeps saying there is an unmatched [ in an f-string, but it is matched
for a class I'm trying to write a python program. One of the lines tries to use an f string to report the value of a key ('cost') inside of a dictionary ('espresso') that is inside another dictionary ...
14
votes
4
answers
2k
views
Why do I need another pair of curly braces when using a variable in a format specifier in Python f-strings?
I'm learning how Python f-strings handle formatting and came across this syntax:
a = 5.123
b = 2.456
width = 10
result = f"The result is {(a + b):<{width}.2f}end"
print(result)
This ...
0
votes
3
answers
252
views
Python f-string equivalent of iterable unpacking by print instruction
print can nicely unpack a tuple removing brackets and commas, e.g.
a = (0, -1, 1)
print(*a)
produces 0 -1 1. Trying the same with f-strings fails:
print(f'{*a}')
The closest f-string option I found ...
1
vote
1
answer
1k
views
Python datetime format: UTC time zone offset with colon
I'm trying to format a datetime object in Python using either the strftime method or an f-string. I would like to include the time zone offset from UTC with a colon between the hour and minute.
...
0
votes
2
answers
514
views
Mypy throws syntax error for multi-line f-strings, despite code running without error
I'm working with Python 3.12 and recently added mypy type-checking to my project. I've encountered an odd issue where mypy throws a syntax error for certain f-strings in my code, specifically those ...