Python, internally, keeps track of your types, which means that the types you use are tracked by python itself without you needing to explicitly declare them. What you are doing by using print('Hello ('+str(num)+')')
is saying to the computer: "print on the screen the following value";
but since there is no sum operation between strings and numbers, you need to convert your number into a string, which is what you are doing by using str(num)
.
This constructs for you a string from the given number; so what you are doing is practically making the value 3 as a number
become the value '3' as a string
;
then the previous line of code, when num == 3
, is equivalent to:
print('Hello (' + '3' + ')' )
which of course now is about a bunch of operations between strings, that are now accepted; here is some more information about python's types handling.
Also, check the answer by Louis-Justin Tallot which provides a solution using f-strings (where f stands for format) that allows you to use the f'{variableName}'
syntax, evaluating the value between the curly brackets directly to a string.