[Tutor] Using formatted string

Manprit Singh manpritsinghece at gmail.com
Wed Oct 27 21:09:19 EDT 2021


Dear Sir,

Suppose i have to print a number using f string , with the only feature
required that it should start with a +  sign if it a positive number or a
- sign if it is a negative number, the number can be an integer or a
floating point number,  Doing it in this way as shown in the examples below
done by me is ok ?
>>> a = -3.659
>>> b= -4
>>> c = 5
>>> d = 5.6
>>> f'{a:+}'
'-3.659'
>>> f'{b:+}'
'-4'
>>> f'{c:+}'
'+5'
>>> f'{d:+}'
'+5.6'
>>>
In the above examples, I have not written type characters (for example f
for float and d for int) after : .
Need your comments.

Regards
Manprit Singh


More information about the Tutor mailing list