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
12
votes
3 answers

Is it possible to reuse an f-string as it is possible with a string and format?

I would like to create an f-string that could be used multiple times as the following code with format: TEXT_AMOUNT = 'text {amount}' def f1(beginning): return beginning + TEXT_AMOUNT.format(amount=10) def f2(ending): return…
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
12
votes
4 answers

Python Sqlite3 - Using f strings for update database function

I have my own personal database I made for fun (so not that concerned with sql injections as its my own private database I made) and am trying to change the functions I created that use string formatting (.format()) and placeholders (?, %s, etc) and…
Cooper
  • 195
  • 1
  • 1
  • 8
11
votes
2 answers

Formatting numbers with same width using f-strings python

I want to format array of numbers with same width using f-strings. Numbers can be both positive or negative. Minimum working example import numpy as np arr = np.random.rand(10) - 0.5 for num in arr: print(f"{num:0.4f}") The result is…
Umang Gupta
  • 15,022
  • 6
  • 48
  • 66
11
votes
2 answers

Why don't f-strings change when variables they reference change?

While playing with new f-strings in the recent Python 3.6 release, I've noticed the following: We create a foo variable with value bar: >>> foo = 'bar' Then, we declare a new variable, which is our f-string, and it should take foo to be…
MaxLunar
  • 653
  • 6
  • 24
10
votes
5 answers

f-string formatting: display number sign?

Basic question about python f-strings, but couldn't find out the answer: how to force sign display of a float or integer number? i.e. what f-string makes 3 displayed as +3?
M. Page
  • 2,694
  • 2
  • 20
  • 35
10
votes
2 answers

Prevent f string from converting float into scientific notation

I was struck by this default behavior of f-strings in python 3.7.2: >> number = 0.0000001 >> string = f"Number: {number}" >> print(string) Number: 1e-07 What I expected was: Number: 0.0000001 This is very annoying especially for creation of…
mrzo
  • 2,002
  • 1
  • 14
  • 26
10
votes
1 answer

Using f-strings to format a boolean

I expect that someone has answered this, but I've searched on this topic and I can't find an answer. Using Python3.6+, I want to format a boolean variable to a fixed width using an f-string. I have tables of results and want a fixed width across…
Hephaestus
  • 4,337
  • 5
  • 35
  • 48
10
votes
3 answers

How to apply float precision (type specifier) in a conditional f-string?

I have the following f-string I want to print out on the condition the variable is available: f"Percent growth: {self.percent_growth if True else 'No data yet'}" Which results in: Percent growth : 0.19824077757643577 So normally I'd use a type…
NoSplitSherlock
  • 605
  • 4
  • 19
10
votes
3 answers

Define a triple-quoted f-string with newline-containing substrings inside a function without outputted indents

I'm trying to pretty-print a HTTP request (that I've mocked here). from typing import NamedTuple class RequestMock(NamedTuple): method = 'POST' url = 'https://bob.com' body = 'body1\nbody2' headers = {'a': '1', 'b': '2'} I have a…
Ben
  • 5,952
  • 4
  • 33
  • 44
10
votes
1 answer

How to write an f-string on multiple lines without introducing unintended whitespace?

Consider the following code snippet: name1 = "Nadya" name2 = "Jim" def print_string(): string = f"{name1}\n\ {name2}" print(string) print_string() which produces Nadya Jim This works, but the 'break' in indentation on the second line of…
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
8
votes
1 answer

How to setup python.logging format using f-string style

I'm writing an app using python 3.7 and want to use only f-strings in it. I'm using python logging module in order to log my application correctly, and I want to use a specific format but the documentation (and the web) only example how to change…
kaki gadol
  • 1,116
  • 1
  • 14
  • 34
8
votes
2 answers

python f'string not working in pd.Series.map function

I have a pd series, s = pd.Series([1, 2, 3, np.nan]) when I do, s.map('this is a string {}'.format) [out] 0 this is a string 1.0 1 this is a string 2.0 2 this is a string 3.0 3 this is a string nan how can I get the same result by…
Pyd
  • 6,017
  • 18
  • 52
  • 109
8
votes
5 answers

Are f-strings supposed to work in Python 3.4?

Is this supposed to work in Python 3.4: >>> a='tttt' >>> print(f'The value of a is {a}')
Quora Feans
  • 928
  • 3
  • 10
  • 21
7
votes
3 answers

How to put backslash escape sequence into f-string

I want to write something as simple as "{}MESSAGE{}".format("\t"*15, "\t"*15) using f"{'\t'*15}MESSAGE{'\t'*15}" # This is incorrect but I get the following error: >>> something = f"{'\t'*6} Weather" File "", line 1 SyntaxError: f-string…
Abhay Jain
  • 119
  • 1
  • 10
7
votes
1 answer

Using python f-string in a json list of data

I need to push data to a website through their API and I'm trying to find a way to use F-string to pass the variable in the data list but couldn't find a way. Here's what I've tried so far: today = datetime.date.today() tomorrow = today +…
locq
  • 301
  • 1
  • 5
  • 22