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

Unsure how to manage my variables/data using tkinter and openpyxl: fstring issue

I am creating an app on a Raspberry Pi (if its at all relevent) which needs to access data from specific cells from a spreadsheet in order to display the settings as button text. There are multiple 'modes', settings and buttons. The button that may…
0
votes
4 answers

Is there a reverse "fstring"?

I need to find a way to call a way to use something with fstring syntax to retrieve information from a string. My goal is to use the same fstring to create a string, and then decode it. For example: def encode(): a = "fizz" b = "buzz" …
Apitronix
  • 305
  • 2
  • 13
0
votes
1 answer

Nested f-strings in Python (Third nested f-string)

I need to convert json to given hcl structure. There is this code: user_dicts = { 'users': [ {'name': 'test1', 'pass': 'password1', 'permissions': [ {'access': 'yes', 'role': 'admin'}, {'access': 'yes', 'role': 'user'} ] }, {'name': 'test2', 'pass':…
RMNTRVN
  • 63
  • 1
  • 5
0
votes
2 answers

How to use escape characters in python fstrings deprecated, without converting to raw string

When running python3.10 -B -Wall testcode.py I get the following deprecation warnings in my code which I would like to fix DeprecationWarning: invalid escape sequence '\:' DeprecationWarning: invalid escape sequence '\:' DeprecationWarning: invalid…
0
votes
2 answers

Problem with alligning my output (f string) Python

I am having some trouble with aligning things in my output. Down below I have copied the expected output and the output my code gives me. It is very similar, however, the numbers aren't aligned correctly. My numbers would align to the right (and…
0
votes
1 answer

Strange behavior when trying to create and write to a text file on macOS

I'm opening a plain text file, parsing it, and adding different lines to existing, empty string variables. I add these variables into a new variable that is a multi-line fstring. Trying to write the data to a new text file is not behaving as…
liquidRock
  • 327
  • 2
  • 12
0
votes
1 answer

Trying to use f' string to print out a list but not the string

I'm trying to print out a list using a following code: list_1 = [1,2,3] list_2 = [1,2,3,4] list_3 = [1,2,3,7,9] list_4 = [1,2,7,11,12,15] for x in range(1,5): print(f'list_{x}') print(list_1) which outputs the…
Jason
  • 35
  • 4
0
votes
1 answer

How can I pad an fstring with dictionary values in

I can use fstring with a dictionary: mydict = {'foo': 'py', 'bar': 'thon'} print(f"I am using {mydict['foo']}{mydict['bar']}") > I am using python I can also pad strings: f"{'Foo': <16} Bar" >'Foo Bar' I want to be able to pad a…
Tim Edwards
  • 1,031
  • 1
  • 13
  • 34
0
votes
1 answer

Indentation appears in the output string when broken it over multiple lines

I have large f-string, and I am braking it (see below), for code readability. but when I print (print to file, it is String.IO object) the string I get the indentation leading each new line (except the first), and I don't want that (my IDE is…
Adam
  • 2,820
  • 1
  • 13
  • 33
0
votes
4 answers

How do you round all decimal places to 2?

Noob to Python and new to Code in general. I "know" how to use the round function and can use it for numbers that have more then 2 decimal places. My question is how do I get numbers that only have 1 decimal place to add the zero to make it to two…
0
votes
2 answers

Lambda f-string for JSON string?

I have a JSON body string in python that contains lorem ipsum e.g. body = '[{ "type": "paragraph", "children": [ { "text": "lorem ipsum" } ] } ]' I want to create a lambda f-string that can take any string and place it where lorem ipsum is. E.g. #…
Chris
  • 500
  • 6
  • 25
0
votes
2 answers

Why do not f-strings work in comprehensions inside other f-strings?

I would like to use an f-string inside a generator which I put inside another f-string, such as here: MORSE_CODE = {'123' : '456', '789' : 'qwe'} print(f'{(f'{key}, {value}' for key, value in MORSE_CODE.items())}') When this was run I got the next…
Vovin
  • 720
  • 4
  • 16
0
votes
1 answer

f string backslash escape

I have a request payload where i need to use a variable to get details from different products payload =…
0
votes
2 answers

Add spaces at the beginning of the print output in python

I'm wondering how Am I suppose to add 4 spaces at the beginnings of the print outputs with f-string or format in python? This is what I use to print now: print('{:<10}'.format('Constant'),'{:{width}.{prec}f}'.format(122, width=10, prec=3)) and my…
0
votes
1 answer

Function argument for an f-string '=' debugging operator

How to pass a variable for an f-string '=' debugging operator? from datetime import datetime def print_var(var): print(str(datetime.now())[:19], end=' ') print(f'{var = }') test = 5 print_var(test) I expect print_var(test) to print the…
OneArb
  • 453
  • 2
  • 14