print('10/3 = ' 10/3)
will directly report a syntax error.
If it is Java language, it will print out 10/3 = 3
. How can I achieve such printing through Python?
I searched some information, and they all said that Python only supports the same type, such as print(True False)
.
How to implement the appeal function?
淡淡烟草味2017-05-17 10:08:52
Yes, but Python is a strongly typed language and does not like automatic type conversion. The fact that you can use '10/3 = ' + str(10/3)
这样子显式转换类型。另外 Python 能够 True + False
is a legacy issue because adding bool values doesn't make sense.
Of course, Python has many good ways to handle string concatenation. The following is the sequence of historical development:
'10/3 = %s' % (10/3)
'10/3 = {}'.format(10/3) # 2.6+
f'10/3 = {10/3}' # 3.6+
PS: To "appeal", you have to fight a lawsuit first, and then if you are dissatisfied with the verdict, you can file an "appeal".
仅有的幸福2017-05-17 10:08:52
print('10/3 =', 10/3)
print('10/3 = {}'.format(10/3))
print('10/3 = %d'%(10/3))
天蓬老师2017-05-17 10:08:52
class myString(str):
def __add__(self,attr):
return ''.join([self.__str__(),'=',str(attr)])
ex=myString('10/3')
print(ex+10/3)
I hope it won’t cause any confusion for you; if it’s ruby, you don’t need to customize the subclass of str, just modify it directly on str, because it’s all “open”, although I’m not very good at ruby