Questions tagged [f-string]
Literal string interpolation in Python
f-string
667
questions
0
votes
1
answer
27
views
need a way to update variable to include all input list items, formatted the same way
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}"){{...
0
votes
0
answers
36
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....
-4
votes
0
answers
61
views
Using f formatting string to get numbers with 2 digital precision after decimal and adding ',' every thousand [duplicate]
I am trying to get the output like:
¥ 968.82
¥ 7,070.94
¥ 3,925.89
¥ 11,958.42
¥ 7,793.94
¥ 2,320.83
¥ 14,452.77
¥ 325.35
¥ 6,333.48
¥ 10,845.00
Here is the code with very helpful ...
1
vote
2
answers
75
views
Regex does or does not give an error depending on which REPL is used
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....
0
votes
3
answers
60
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
3
answers
90
views
Why is format() throwing ValueError: Unknown format code 'f' for object of type 'str' when I'm not inputting a string?
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 ...
-6
votes
1
answer
138
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
vote
2
answers
87
views
Is there a Python f-string equivalent of C++’s std::quoted?
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 <&...
-1
votes
3
answers
65
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 ...
0
votes
1
answer
84
views
What does '!a' do in python f-strings? [duplicate]
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 ...
-1
votes
1
answer
40
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 ...
0
votes
2
answers
57
views
Problem with Python f-string formatting of struct_time
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 ...
0
votes
2
answers
130
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 ...
0
votes
0
answers
36
views
Python logging: check if a log will print before doing f-string?
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....
-1
votes
1
answer
61
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
...