print('%s is (%d+10) years old' % ('Joe', 42))
Output: Joe is (42+10) years old
Expected output: Joe is 52 years old
Using % instead of {} as placeholders in Python Oftentimes, we see % used as placeholders in Python code. This can be used as a placeholder instead of {} .
In, Python %s and %d are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator. This code will print abc 2.
The %d operator is used as a placeholder to specify integer values, decimals or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.
To check if the variable is an integer in Python, we will use isinstance() which will return a boolean value whether a variable is of type integer or not. After writing the above code (python check if the variable is an integer), Ones you will print ” isinstance() “ then the output will appear as a “ True ”.
You can do this with f-strings in Python 3.6+.
name = "Joe"
age = 42
print(f'{name} is {age + 10} years old')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With