Can't + in python be used as a connector like java?
迷茫
迷茫 2017-05-17 10:06:52
0
3
626

print('10/3 = ' 10/3)will directly report a syntax error.
If it is Java language, it will print out10/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 asprint(True False).
How to implement the appeal function?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (3)
淡淡烟草味

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 + Falseis 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".

    仅有的幸福
    print('10/3 =', 10/3) print('10/3 = {}'.format(10/3)) print('10/3 = %d'%(10/3))
      Peter_Zhu

      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

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!