Weak Type Ability for Python

MRAB python at mrabarnett.plus.com
Wed Apr 12 15:11:32 EDT 2023


On 2023-04-12 19:57, Mats Wichmann wrote:
> On 4/12/23 11:11, Chris Angelico wrote:
>> On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
>> <ali.mohseniroodbari at gmail.com> wrote:
>>>
>>> Hi all,
>>> Please make this command for Python (if possible):
>>>
>>>>>> x=1
>>>>>> y='a'
>>>>>> wprint (x+y)
>>>>>> 1a
>>>
>>> In fact make a new type of print command which can print and show strings
>>> and integers together.
>>>
>> 
>> Try:
>> 
>> print(x, y)
>> 
>> ChrisA
> 
> 
> To continue on, what do you want "addition" of dissimilar types to do -
> since that's what you have asked for above.
> 
> You can write yourself an object which is happy with certain
> combinations, so you don't have this scenario:
> 
>   >>> x + y
> Traceback (most recent call last):
>     File "<input>", line 1, in <module>
>       x + y
>        ~~^~~
> TypeError: can only concatenate str (not "int") to str
>   >>> y + x
> Traceback (most recent call last):
>     File "<input>", line 1, in <module>
>       y + x
>        ~~^~~
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>   >>>
> 
> 
> Or you can help out the print function by doing some of the fiddling
> yourself:
> 
>   >>> print(f"{x}{y}")
> 1a
> 
Or:

>>> print(x, y, sep='')
1a


More information about the Python-list mailing list