Skip to main content
Filter by
Sorted by
Tagged with
1 vote
1 answer
162 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
198 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
89 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,901
2 votes
1 answer
95 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
72 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,650
2 votes
1 answer
125 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
66 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
3 votes
3 answers
128 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
  • 1,016
0 votes
4 answers
158 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
335 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
229 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,650
2 votes
1 answer
865 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
479 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
191 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
-2 votes
2 answers
147 views

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
209 views

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
65 views

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
108 views

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,115
0 votes
1 answer
46 views

I'm running a program in python that sends graphql post requests. The query looks like this, : var = input("string here") query = f"""query{{item1(item2:"{var}"){{...
discord alt's user avatar
1 vote
1 answer
315 views

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....
emefff's user avatar
  • 11
1 vote
2 answers
98 views

Consider the following Python code using the regex library re import re re.compile(rf"{''.join(['{c}\\s*' for c in 'qqqq'])}") I have run it in two different REPLs: https://www....
Mathew's user avatar
  • 1,600
0 votes
3 answers
91 views

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 ...
Valiot's user avatar
  • 1
3 votes
3 answers
905 views

I am using Python 2.7. (Switching to Python 3 for this particular code is not an option, please don't suggest it.) I am writing unit tests for some code. Here is the relevant piece of code: class ...
Réka's user avatar
  • 145
-5 votes
1 answer
3k views

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 ...
Duarte Almeida's user avatar
1 vote
2 answers
124 views

In C++, the std::quoted stream manipulator produces a quoted and escaped version of a string: std::string dirPath = "C:\\Documents\\Oval Office Recordings\\Launch Codes.txt"; std::cout <&...
templatetypedef's user avatar
-1 votes
3 answers
99 views

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 ...
KamilCuk's user avatar
  • 146k
3 votes
1 answer
1k views

When messing around in python with the exclamation mark, it said that it has to be followed by either !a, !s, or !r. What does !a do? Like can you do this? f"{"txt"!a}" What will ...
Cycro the large planet's user avatar
-1 votes
1 answer
52 views

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 ...
MJP's user avatar
  • 99
1 vote
2 answers
120 views

I have the following code; oStat=os.stat(oFile) print(time.strftime('%H:%M:%S', time.localtime(oStat.st_mtime))) print(f"{time.localtime(oStat.st_mtime):%H:%M:%S}") The first print ...
Hipponax43's user avatar
3 votes
2 answers
4k views

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 ...
pktl2k's user avatar
  • 670
0 votes
0 answers
85 views

Suppose I have code I want to be able to run in production or verbose mode. In verbose, I want to print debug information that will slow the program down: import logging import time import sys logging....
Kaia's user avatar
  • 909
-1 votes
1 answer
129 views

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 ...
m.rp's user avatar
  • 748
0 votes
3 answers
202 views

I have a long f-string with containing many float values, all of which I want to round to the same number of digits. Is there a way to do this in one go or in a smart way, or do I need to specify the ...
db_'s user avatar
  • 653
-2 votes
1 answer
194 views

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)...
user2875230's user avatar
0 votes
0 answers
30 views

I want to enclose each element of a list with double quotes instead of single? With f-string I'm not able to get double quotes and when I try solutions from the internet it's applying double quotes to ...
Rohith Rathod's user avatar
-1 votes
1 answer
48 views

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 ...
Ruslan Efanov's user avatar
1 vote
1 answer
991 views

MY_WIDTH = 80 MY_CONSTRUCTED_HDR = f"this is a hdr of length {MY_WIDTH}" print(MY_CONSTRUCTED_HDR) this is a hdr of length 80 # YES, but NOT what I need -- see below I want something like ...
doojinsi's user avatar
0 votes
1 answer
106 views

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&...
realityChemist's user avatar
0 votes
1 answer
58 views

My Code: videos = [] for keys in data: videos.append({f"'path: 'videos/{filename}.mp4', 'description': '{filename}'"},) The problem: problem As you can see the f-string is putting the &...
Francisco Antunes's user avatar
0 votes
1 answer
97 views

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 ...
mecook112's user avatar
1 vote
2 answers
186 views

It is nice, easy and fast to use format-strings in python. So I never considered the performance penalty of this operation. Some time ago I switched my program from float data type to Decimal to ...
StarterKit's user avatar
0 votes
2 answers
76 views

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 ...
Liam's user avatar
  • 11
1 vote
2 answers
226 views

I am creating a string using f string and need to avoid fields which are None. I am currently using below code def output(self) -> str: data = '' data += 'Books: [\n' for ...
VN-'s user avatar
  • 75
0 votes
1 answer
37 views

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 ...
thepen's user avatar
  • 1
0 votes
1 answer
293 views

I am trying to run a basic SQL query to UPDATE the value for schoolNum from a database called bartonHill, using f strings to put the values in import sqlite3 name = [input('enter your firstname: ')] ...
HurricaneShard's user avatar
1 vote
1 answer
114 views

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....
GoodFish's user avatar
0 votes
1 answer
88 views

Hello I was practicing using loops and I saw that while using concatenation I have to convert int's to strings, but then I saw that if I use a f-string I dont need convert it to an int. Here is the ...
Noot Noot's user avatar
-1 votes
3 answers
255 views

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!' ...
Emse's user avatar
  • 3
0 votes
1 answer
149 views

Question question regarding f-strings and expressions in Python 3. The Python documentation states clearly that: Formatted string literals (also called f-strings for short) let you include the value ...
the_slug's user avatar
  • 127

1
2 3 4 5
14