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
2 answers

Python: Custom print function that prints function text

Python script: text = "abcde" print("text[::-1] : ", text[::-1]) print("text[5:0:-1] : ", text[5:0:-1]) Output: text[::-1] : edcba text[5:0:-1] : edcb Can a custom function be defined that avoids duplicity in typing? For ex: text = "abcde" def…
user9329768
0
votes
2 answers

Using '{a..b}' wildcard in fstring

I'm looking to use xarray.openmfdataset to open a specific set of files. For example, I'd like to open file.20180101.nc, file.20180102.nc, file20180103.nc with the following code: xr.open_mfdataset('./file.{20180101..20180103}.nc',…
lsterzinger
  • 687
  • 5
  • 22
0
votes
0 answers

python f string equivalent for .format() when formatting a string variable containing variables to format

I've read PEP498 and multiple articles, forums, etc and finally reached a point when I am asking you ppl for help. Yes, I accept a possibility that maybe I simply did not understand what I read :) so would appreciate that pointing out too ;P First…
Krystian
  • 1
  • 1
0
votes
1 answer

Why does f"{11: 2d}" not pad to 2 characters?

I'm using Ipython 7.0.1 with Python 3.6.9 I want to right align numbers with the format string mini language. I use 1 and 11 as examples for 1 and 2 digit numbers which I want to pad to 2 characters total. For zero padding everything works as…
peer
  • 4,171
  • 8
  • 42
  • 73
0
votes
0 answers

Skip a whole file conditionally with doctest

I wrote a sphinx .rst file whith doctest snippets using f-strings. I use tox to test my project and my documentation with several versions of python, and pytest to run the tests with pytest --doctest-glob='*.rst'. Some of the python versions I run…
azmeuk
  • 4,026
  • 3
  • 37
  • 64
0
votes
3 answers

print with f-string formatting on float - unable to get how the output is generated

x = 100.1205 y = str(x)[6] which means y=0 When try to execute below code output is 100. print ("{:.{}f}".format(x,y)) Can anyone tell how it is giving 100 as output.
sre
  • 249
  • 1
  • 12
0
votes
4 answers

Add to dictionary with an f-string inside a for loop

I'm currently trying to do something very similar to: for letter in ['a', 'b', 'c']: key1 = f'{letter}_1' key2 = f'{letter}_2' numbers = { key1: 1, key2: 2 } I would expect numbers to be: {'a_1': 1, 'a_2': 2,…
Albatross
  • 955
  • 1
  • 7
  • 13
0
votes
0 answers

Raw f-string SyntaxError

I'm trying to build a regex with a raw f-string in a Sublime Text 3.2.2 plugin: operatorSymbols = "==|!=" print(fr'\s*({operatorSymbols})\s*') Desired output: \s*(==|!=)\s* I get the error: print(fr'\s*({operatorSymbols})\s*') …
0
votes
1 answer

How to do alignment on hex in string formatting

Is there a way to align hexadecimal digits in string formatting? I feel like it has to be quite easy, and I'm just missing the formatting. For example: ones_comp = 72510 print (f"2. Ones comp: {ones_comp:#0x>12}") I'd like it to print something…
David542
  • 104,438
  • 178
  • 489
  • 842
0
votes
1 answer

Apply f-string interpolation to a string

Is there a way to achieve applying f-strings interpolation dynamically on a string? For example given the string a = '{len([1])}' a.interpolate() to give '1'
hangc
  • 4,730
  • 10
  • 33
  • 66
0
votes
0 answers

Why cannot Sublime Text understand f-strings?

class Stats: def __init__( self, hp, hunger, dehydration, phys_strength, mental_strength, psychic_powers, intelligence, ): self.hp = hp self.hunger = hunger …
Caspian Ahlberg
  • 934
  • 10
  • 19
0
votes
1 answer

Why aren't strings allowed to be embedded into f-strings just as values?

print(f"my age is {54} and my name is {True}") yields my age is 54 and my name is True and the following also works print(f"my age is {54.0} and my name is {True}") However, when I place a string inside the curly brackets: print(f"my age is…
stellar
  • 53
  • 10
0
votes
3 answers

F String With Special Characters

I want to print the following using an f string literal. It will be running wihtin a function and apples will be one of the arguments. I want it to have the square brackets included. "I like [apples] because they are green" I have tried the…
machinelearner07
  • 113
  • 1
  • 1
  • 12
0
votes
0 answers

how to use f-string to round up decimal 2 position

anyone know how f-string roundup work, I want to around to decimal 2 position but I found out f-string does not round up for decimal 2 position >>> a = 0.1235 >>> f'{a:.3f}' '0.124' #as expect >>> a = 0.125 >>> f'{a:.2f}' '0.12' # why not 0.13? >>>
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
0
votes
1 answer

What benefits does the % formatter offer in comparison to f strings?

I was going through the fluent python book when I came across the following code def tag(name, *content, cls=None, **attrs): """Generate one or more HTML tags""" if cls is not None: attrs['class'] = cls if attrs: attr_str…