Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python type error | operator behaviour with str and int

print("*" * 10) this line in python print * 10 times.

However the when trying to print("*" + 10) there is a type error.

Why is there a difference in operator behaviour with str and int

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s because + is a concatenator in Python, and you can concatenate two strings but not a string and an int. You can, of course, cast the int into a string and concatenate that with another string if that’s what you want to do: print("*" + (str(10)))

The thing works with * because that operator will simply concatenate the string with itself as many times as indicated by the integer number, as you’ve observed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading