I am wondering how I could convert len of a variable consisting of a list into a string using f-string. An example is this:
str(len(names))
.
I tried this with the curly braces like this
{str(len(names)}
and other variations but it doesn't work. I would very much appreciate your help. Many thanks in advance.
Asked
Active
Viewed 51 times
-3

Mica
- 1
- 3
- 5
-
`f'{len(names)}'`…?! – deceze Nov 25 '21 at 16:11
-
1You left off the final `)` in both examples, which makes me suspect a mistake when you tried it (having failed to provide an actual [MCVE] of your attempt, we have no way to know what you actually tried). – ShadowRanger Nov 25 '21 at 16:12
-
I corrected the typo. And yes, your answer solved my problem too. – Mica Nov 26 '21 at 17:09
1 Answers
1
It's pretty straightforward. The explicit conversion with str
is unnecessary.
>>> names = ['foo', 'bar']
>>> f"{len(names)}"
'2'
>>>

Chris
- 26,361
- 5
- 21
- 42