Questions tagged [f-string]

Literal string interpolation in Python

An f-string is the abbreviated name for a "formatted string" syntax that was introduced in Python 3.6 and extended in Python 3.7. They enable literal string interpolation:

>>> value = 80
>>> f'The value is {value}.'
'The value is 80.'

See PEP 498 for a comprehensive description of the feature, and also PEP 536 for the final grammar.

621 questions
0
votes
1 answer

how to use fstring in a complex json object

Is there a way to use fstring to change variable dynamically in a complex json object like this: payload = json.dumps({ "query": "query ($network: EthereumNetwork!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {\n …
Bemz
  • 129
  • 1
  • 16
0
votes
3 answers

Convert a dynamically sized list to a f string

I'm trying to take a list that can be size 1 or greater and convert it to a string with formatting "val1, val2, val3 and val4" where you can have different list lengths and the last value will be formatted with an and before it instead of a…
0
votes
0 answers

How to use f-Literal with PartiQL in AWS and boto3 on a Secondary Index

I want to query from a secondary index with PartiQL. Querys from the basetable work fine. But when trying it on a Secondary Index i get an empty list I tried the following: table_name_2 = "datatable.GSI1" pk = 'U-0001' stmt = f"SELECT * FROM…
0
votes
1 answer

f-string with percent and fixed decimals?

I know that I can to the following. But is it possible to combine them to give me a percent with fixed decimal? >>> print(f'{0.123:%}') 12.300000% >>> print(f'{0.123:.2f}') 0.12 But what I want is this output: 12.30%
tturbo
  • 680
  • 5
  • 16
0
votes
0 answers

A clean way of combining r and f strings, with multiple escapes?

I have no idea if my Python is on the right track here, but with this example, I need to combine both r and f-strings whilst escaping a handful of characters in a particularly ugly command. import subprocess cust = input("Cust:…
pythonInRelay
  • 99
  • 1
  • 8
0
votes
1 answer

How would I convert a format string with quotes into an f-string with nested quotes?

I've seen this question but I think I have more nested quotes and it's doing my head in. How would I convert the following line into an f-string? file.write('python -c "{code}"'.format(code="open('test.txt', 'w');"))
Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
0
votes
1 answer

Get variables from function input and text after

So id like to make varibles from my function input So basically, the if statement should check if the varible is True or not. And i want to use the function input + ”-outcome” to check. Function-outcome = True Function2-outcome = False c def a(B): …
0
votes
2 answers

Is it possible to use whitespace in format specifier

If I have following print statements: print("#"*80) print(f"##{'':.^76}##") print(f"##{'Hello World':.^76}##") print(f"##{'':.^76}##") print("#"*80) I will get a nice border around my "Hello World" but with…
MaKaNu
  • 762
  • 8
  • 25
0
votes
1 answer

Python: Limit the maximum amount of decimals in all print calls

I have a script with a lot of print calls. I want to limit all the printed numbers to a maximum of 5 decimals, but changing it by hand with f_strings or formatting would be a lot of work. Is there any way to specify at the beginning of the script…
0
votes
0 answers

subprocess, curl - Failed to open/read local data from file (graph API)

I try to uplaod media on FB via subprocess and curl, it seems there is some problem with the formatting of the fstrings that pass the args as fstring into the command. My original working curl command looks like this: curl -F 'source=@output.mp4' -F…
0
votes
1 answer

remove trailing zeros in float representation python

Using the f-string floating formatting I get the following results: f"{1.34 :.2f}" # 1.34 f"{1.30 :.2f}" # 1.30 f"{1.00 :.2f}" # 1.00 What I want however is to remove all trailing zeros: f"{1.34 :.2f}" # 1.34 f"{1.30 :.2f}" # 1.3 f"{1.00…
Johan Tuls
  • 105
  • 7
0
votes
2 answers

LaTex - Is it possible to display nice equations, and normal text together while using display(Math())?

Can I print normal text while using display(Math()) with F-string? I would like to use F-string and mix it with nice equations, but I can't figure out how to print normal text. As you can see in the first image the text isn't normal(font). I also…
N00bDev
  • 67
  • 8
0
votes
0 answers

How do I add a variable to a raw string within a loop?

I have the following chunk of code: for i in range(len(files)): if files[i].split('^')[0]=='SequenceFile': number = files[i].split('^')[5] file = files[i].split('^')[2] src_path =…
0
votes
1 answer

Can you insert anything other than a variable in {curly braces} of an f string literal?

I was writing a print statement to test code. But I realised I mistakenly had put a (self-defined) object (from a different class) in the {}. I have already add the str method in the object to be able to call off print on it. I tried to see if…
er-ads
  • 23
  • 3
0
votes
3 answers

List comprehension using f-strings

I have three variables a = 1 b = 2 c = 3 and I want to have a string like 'a=1, b=2, c=3' so, I use f-string, x = '' for i in [a, b, c]: x += f"{i=}" but it gives, x 'i=1, i=2, i=3, ' how do I make the i to be a, b, and c?
apostofes
  • 2,959
  • 5
  • 16
  • 31