4

Is it possible to use Python f-string with a variable decimal number specifier? Here is an example, but it does not work:

print(f'{1.23456:.2f}')    # working f-string code

number = 2
print(f'{1.23456:.' + number + 'f}')

Python3.7.

2
  • What is the expected output? Commented Apr 23, 2021 at 13:37
  • 1
    print(f"1.2345:.{number}f")? Commented Apr 23, 2021 at 13:37

1 Answer 1

5

You put a second set of brackets inside the first one:

number = 2
print(f'{1.23456:.{number}f}')
# 1.23
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.