Skip to main content
1 vote
1 answer
150 views

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 ...
Matteo's user avatar
  • 93
3 votes
2 answers
193 views

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 ...
Antoine Gallix's user avatar
1 vote
2 answers
87 views

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}$&...
Qiang Xu's user avatar
  • 4,881
2 votes
1 answer
92 views

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&...
K4zo's user avatar
  • 33
3 votes
2 answers
71 views

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=}...
Paul Jurczak's user avatar
  • 8,628
2 votes
1 answer
119 views

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 ...
Noushadali's user avatar
1 vote
1 answer
62 views

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}...
Kiffikiffe's user avatar
2 votes
3 answers
117 views

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-...
patraulea's user avatar
  • 986
0 votes
4 answers
150 views

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 ...
BSachin's user avatar
  • 21
0 votes
2 answers
308 views

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 ...
user3573647's user avatar
14 votes
4 answers
2k views

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 ...
S_D's user avatar
  • 182
0 votes
3 answers
223 views

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 ...
Paul Jurczak's user avatar
  • 8,628
2 votes
1 answer
830 views

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. ...
E_Cross's user avatar
  • 43
0 votes
2 answers
460 views

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 ...
Cornelius Roemer's user avatar
1 vote
5 answers
185 views

I want to create an f-string that combines two strings, prepending a '.' character to the second if it is not empty. reference = "file" modifier = "read" print(f"unknown ...
creative-resort's user avatar

15 30 50 per page
1
2 3 4 5
46