Skip to main content
3 votes
2 answers
59 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=}...
Paul Jurczak's user avatar
  • 8,235
2 votes
1 answer
77 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 ...
Noushadali's user avatar
1 vote
1 answer
47 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}...
Kiffikiffe's user avatar
2 votes
3 answers
79 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-...
patraulea's user avatar
  • 916
0 votes
4 answers
95 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 ...
BSachin's user avatar
  • 21
0 votes
2 answers
155 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 ...
user3573647's user avatar
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 ...
S_D's user avatar
  • 182
0 votes
3 answers
164 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 ...
Paul Jurczak's user avatar
  • 8,235
2 votes
1 answer
412 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. ...
E_Cross's user avatar
  • 43
0 votes
2 answers
336 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 ...
Cornelius Roemer's user avatar
1 vote
5 answers
153 views

F-string format for conditionally prefixing a value

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
-2 votes
2 answers
129 views

Error in syntax of fstrings in function input() [closed]

I have the following code in Python 3 def inputWithDefault(text:str, default): result = input(f"{" " * int(len(text))}{default}\r{text}") return default if not(result is ...
JK Bot's user avatar
  • 37
0 votes
1 answer
136 views

Python>=3.8, pylint>=3.2.7 - Why doesn't local pylint raise an `f-string: unmatched '('` error here?

I'm confused as to why a local pylint check ignores the following error, but an apparently identical pylint check as part of a GitHub Actions workflow raises a syntax error. I have a small Python test ...
semuadmin's user avatar
1 vote
1 answer
58 views

For loop Teradata insert statement

I am pretty new to Python and looking for some assistance. I have a large number of Teradaat tables and I am looking to take a single field from each of theose tables and put it into a new table. ...
Richard Grant's user avatar
0 votes
2 answers
75 views

How can I set a python f-string format programmatically?

I would like to set the formatting of a large integer in an fstring programmatically. Specifically, I have an integer that can span several orders of magnitude and I'd like to print it using the ...
Ziofil's user avatar
  • 2,137

15 30 50 per page
1
2 3 4 5
46