dEitY719
dEitY719

Reputation: 1

What is wrong f-string in Python?

import numpy as np
a =np.array([1, 3, 0, 2], int)
b =np.array([5, 2, 1, 2], int)
print(f'{a > b = }')
a > b = array([False,  True, False, False])

import numpy as np
a =np.array([1, 3, 0, 2], int)
b =np.array([5, 2, 1, 2], int)

def myprn(text):
    print(f'{text = }')

myprn(a > b)
text = array([False,  True, False, False])
#^^^ how to fix this requirement?

Upvotes: -1

Views: 358

Answers (2)

dEitY719
dEitY719

Reputation: 1

Thanks to @Tomerikoo.

  • His reply gave me a hint. I was able to learn the movements I wanted according to his comments

@Tomerikoo's comment

The only way I can think to make something similar to this work is print(f'{text} = {eval(text)}') and then passing the function strings with the expression, i.e. myprn('a > b')

Upvotes: -1

sudden_appearance
sudden_appearance

Reputation: 2195

You can't, = in f-string is self-documenting sign which just prints expression before it as is. Doc reference

Upvotes: 2

Related Questions