# Print() behaves differently when using formatted string

**URL:** <https://discuss.pytorch.org/t/print-behaves-differently-when-using-formatted-string/192555>\
**Category:** PyTorch Live\
**Created:** [November 25, 2023, 8:15am UTC](https://discuss.pytorch.org/t/print-behaves-differently-when-using-formatted-string/192555 "2023-11-25T08:15:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![preshel](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@preshel](https://discuss.pytorch.org/u/preshel)\
**Post date:** [November 25, 2023, 8:15am UTC](https://discuss.pytorch.org/t/print-behaves-differently-when-using-formatted-string/192555/1 "2023-11-25T08:15:22Z")

</div>

I have a pytorch tensor array named yTensor.  
I was created from Pandas DF as so  
yTensor = torch.tensor(dfY.values, dtype=torch.float32).to(device)

When I print an item such as  
print(yTensor[0]), it prints “tensor(0., device=‘cuda:0’)”  
But when I print it in formatted form  
print(f’yTensor[0]={yTensor[0]}'), it prints “0.0”

Is that by design or some sort of bug?

---

<div class="post-metadata">

**Author:** ![tom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/32/3162_2.png) [@tom](https://discuss.pytorch.org/u/tom)\
**Post date:** [November 27, 2023, 8:50am UTC](https://discuss.pytorch.org/t/print-behaves-differently-when-using-formatted-string/192555/2 "2023-11-27T08:50:10Z")

</div>

It’s more or less by design and the different ways Python creates representatrions: printing a tensor directly uses the ` __str__ ` method under the hood while `f"{tensor}"` will use the ` __format__ ` method (if it exists). PyTorch implements both and the output is slightly different for scalar tensors.

Best regards

Thomas
