686 questions
1
vote
1
answer
145
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 ...
1
vote
2
answers
87
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}$&...
3
votes
2
answers
70
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=}...
1
vote
1
answer
62
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}...
0
votes
4
answers
146
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 ...
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 ...
2
votes
1
answer
816
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.
...
1
vote
5
answers
184
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 ...
0
votes
1
answer
199
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 ...
0
votes
2
answers
102
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 ...
1
vote
1
answer
302
views
Why print(f"{variable = }") in Python 3.12 also prints numpy type?
I noticed, that with Python 3.12 and a numpy.float64 when I simply do a
x = numpy.sqrt(3) # type is <class 'numpy.float64'> now
print(f"{x = }")
# this is printed
>>>x = np....
0
votes
3
answers
85
views
Difference between format(**locals()) and f-string when using variable as key for dictionaries
I have a string templ with variable part that I have to convert with local variables. Variable part of string have a dictionary cur_cols_dct from which I want to get a single value. Key of a ...
-5
votes
1
answer
3k
views
Unterminated f-string literal [closed]
The following code
message = (
f'Lembrete de que existe uma reserva para amanhã de *{dayAfter}*
{dayAfterCellphone} para a carrinha que estás a usar.'
)
is giving me ...
-1
votes
3
answers
97
views
What is the rationale for specifying dynamic width and precision _after_ the argument in str.format?
In C programming language dynamic field width and precision is specified before the argument.
printf("%.*s", 2, "ABCDEF")
Yet when str.format was introduced in Python3.0 in 2008 ...
-1
votes
1
answer
51
views
Return Output from Function on Multiple Lines
I've tried to figure this out on my own, but I'm coming to a dead end. I'm trying to return a string from a function that I created that references amounts derived from a pandas dataframe that was ...
2
votes
2
answers
4k
views
Nested quotes in f-string with Python 3.12 vs older versions [duplicate]
With Python 3.12 I can do the following without error:
a = "abc"
s = f"{a.replace("b", "x")}"
Note the nested " characters.
With Python 3.6 the same will ...
-1
votes
1
answer
125
views
Key Error when having spaces and "\n" in multiline f-string
Trying to format SPARQL queries in python using f-strings in order to build different queries from a template,
but i'm getting KeyError when i try to format a multiline string:
query = f'''SELECT ?age
...
-2
votes
1
answer
187
views
Use f-string instead of format call [duplicate]
The server I'm posting ym data to doesn't seem to support format call so I get the folowing error.
Use f-string instead of 'format' call for the functions below.
def write_xlsx_response(self, queryset)...
-1
votes
1
answer
45
views
Get error when try to put string-funcs in f-string [duplicate]
a = 'guindo , barwos, zasport'
a
guindo , barwos, zasport
But when I write
print(f'find is {a.find('a')}')
I get error
SyntaxError: f-string: unmatched '('
Why? Can someone please write right ...
0
votes
1
answer
105
views
Why does comparison between empty ndarrays and empty lists behave differently if done inside an f-string? [closed]
I was messing around a bit with empty ndarrays and uncovered some unexpected behavior. Minimal working example:
import numpy as np
print(f"{np.empty(0)} :: {type(np.empty(0))}")
print(f&...
0
votes
1
answer
96
views
How to convert Python2 string with formatting into the new f string used in python3
I am working on converting some Python 2.7 code to reflect a more Python 3.10 method and came across this scenario. A string variable is holding the formatting and then later on variables are added to ...
0
votes
2
answers
76
views
Python file wont write blank new line
I'm making these report files and am trying to append a blank line in between each cycle of my loop, just to help with readability.
Usually, I can just open the file and f.write('\n') to do this - for ...
0
votes
1
answer
37
views
Python: save output file with the logged-in username appended to the file name
I have a python program that creates an output file. That all works correctly.
However, I'd now like to save the python output file with a "_username" appended to the filename.
I have ...
1
vote
1
answer
107
views
Iterating through a list of countries to pull data from Datastream
I am pulling data from Datastream for a project. I have a list of 40 countries for which I want their bond issuance information.
The query is as follows:
All1 = rd.discovery.search(
view = rd....
-1
votes
3
answers
253
views
Automatically turn a string into a formatted string? (Python)
I'd like to program an alternate print() function, that automatically turns the inputted string into a formatted one, with correct formatting for variables.
So why doesn't this work?
Test = 'Hello!'
...