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

Why f'{None}_some_string' not throw an error?

I wonder why f'{None}_some_string' not throw an error? Here is some example to reproduce the problem: s1 = None # s2 = 'real_string' # s1 + s2 # TypeError: unsupported operand type(s) for +: 'NoneType' and…
mrgloom
  • 20,061
  • 36
  • 171
  • 301
0
votes
1 answer

can you define __doc__ directly?

I would like my docstring to be an f-string to put information on, but if I try to do that it isn't showed when you type foo.__doc__ or help(foo), so I tried the following: used .format() which didn't register as a docstring tried to do self.__doc__…
Arale
  • 39
  • 1
  • 8
0
votes
1 answer

python f-string include all numbers except 0 while making a file list

I want to make a list of files with extend() while ignoring any file name ending with 0 as in _file0.h5. I have this line for concatenating all the files into a list data_files_0.extend(sorted(glob(f"{directory}*_file{filenum}.h5") ) ) I am trying…
mrq
  • 274
  • 1
  • 2
  • 14
0
votes
2 answers

Using lists to generate url

I am currently working on a program to help manage my comic book collection. I want to take the title, issue number, and grade of each comic in its corresponding list and use the values to fill in the f string portions of a url to get last sale…
Kevin Epps
  • 13
  • 1
0
votes
1 answer

What is the difference between .2 and .2f format specifiers?

Consider following snippet: for value in [1.123, 1.126, 1.12, 1.16, 1.1, 1.0, 12345.6789]: print( f'Testing {value:>12}', f'.2 {value:>17.2}', f'.2g {value:>16.2g}', f'.2f {value:>16.2f}', end='\n\n', …
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
0
votes
2 answers

trying to use multiple f strings in discord python bot not working

Hello so I have been trying to fox this for a few hours now and just cant figure out what is wrong, I have made api calls in discord python before but this is the first time using multiple variables like this. @client.command() async def…
3301
  • 23
  • 3
0
votes
2 answers

Python f-string in jupyter notebook in loop not working

I'm using python v3.8 with jupyter lab notebook, and I'm having problems with using f-string instead of regular print in a loop. When I write in one cell a=2 f" a={a}"\ f" a={a+1}+1 " the output is ' a=2 a=3+1 ' (and without that 'back slash'…
lugger1
  • 1,843
  • 3
  • 22
  • 31
0
votes
1 answer

Python f-string surprising results on floats

I am trying to format float numbers in a fixed point notation: x.xxx, three digits following the decimal point regardless of the value of the number. I am getting surprising results. The first in particular would suggest that it is giving me three…
Joymaker
  • 813
  • 1
  • 9
  • 23
0
votes
0 answers

Inserting an image in Outlook using html and a nested f-string

I am trying to embed some images into an email in Outlook using python. However, the images do not appear, but instead a box stating "The image cannot be displayed. Verify that the link points to the correct file and location." The link is correct…
Omar
  • 27
  • 5
0
votes
1 answer

python f-string with dictionary literal

How to do a dictionary literal within the f-string curly braces? d_all = {d:d+2 for d in range(10)} keys = [2,5] tmp_dict = {d:d_all[d] for d in keys} print(f'values: {tmp_dict}') print(f'keys: {[d for d in keys]}') # list works print(f'dict: {…
fivelements
  • 1,487
  • 2
  • 19
  • 32
0
votes
1 answer

Specifying width of concatenated variables in a f-string

I'm using the loguru package for logging in my python project. https://github.com/Delgan/loguru I'm looking to override the default format so I can specify a fixed with for the module / function / line number string. Here's the default format…
Tyler Weiss
  • 139
  • 1
  • 15
0
votes
1 answer

F Strings and Interpolation using a properties file

I have a simple python app and i'm trying to combine bunch of output messages to standardize output to the user. I've created a properties file for this, and it looks similar to the following: [migration_prepare] console=The migration prepare phase…
0
votes
3 answers

Why does Python `f-string` + inline for loop create a generator when they're passed as parameter?

I found this example on the internet def format_attributes(**attributes): """Return a string of comma-separated key-value pairs.""" return ", ".join( f"{param}: {value}" for param, value in attributes.items() ) The…
Turtlean
  • 579
  • 4
  • 9
0
votes
1 answer

How can you put dict and get method inside an f-string?

I have this dictionary that I want to embed inside an f-string to shorten up my code (for golfing purposes). Is there any way to include the braces inside the f-string braces? # what I have: i=2 a={1:'a',2:'b',3:'c'}.get(i,'g') b=f'{a} is the…
teepee
  • 2,620
  • 2
  • 22
  • 47
0
votes
0 answers

f-string reverting a rounded float to un-rounded

I have a function (containing numpy calculations, float32) which returns a dictionary, where the values are lists containing text and numbers. Within the function certain numbers have been rounded to three decimal places as they are added to the…