Python: print(...{}' .format) or print(...+str(...))

Asked

Viewed 3,029 times

-1

code version 1

tempF = input('Temperature in degrees Farenheit: ')

tempC = (5 * (int(tempF) - 32) / 9)

print('Temperature in degrees Celsius: ' + str(tempC))

In the above code (#1), to display the final message, I have to convert the tempC variable to string and the code works as expected.


code version 2

tempF = input('Temperature in degrees Farenheit: ')

tempC = (5 * (int(tempF) - 32) / 9)

print('Temperature in degrees Celsius: {}' . format(tempC))

In the above code (#2), to display the final message, use the . format without converting the tempC variable to string and the code works as expected.

What are the reasons for both codes to work? Any notes related to performance? Python version? Obsolete commands? Which code version is recommended?

  • I don’t know if it gets duplicated, but there’s an explanation here. Basically, format ends up calling str "behind the scenes"

2 answers

0

In the first case, the following operations are performed. 1º Captures a value in the "string" type. 2º Converts the captured value to "integer", inserts it in the formula, performs the calculation and stores the result in the variable "tempC" converted to "float" (implicit conversion. For every "/" division operation produces a float value). 3º Converts the value of the stored variable to "tempC" for "string" and then displays it.

In the second case, the following operations are performed. 1º Captures a value in the "string" type. 2º Convert the captured value to "integer", insert it in the formula, perform the calculation and store the result in the variable "tempC" converted to "float" (implicit conversion, as I said). 3º Calls the "format" function (although it is not passing the formatting parameters) to display the value of the variable stored in "tempC".

In the first case, you are using the "str()" function twice (separately). In the second case, you are only using once the function "str()".

Converting a variable to a string requires more computational resources. When you call this function two or more times in an algorithm, you make it less efficient.

For programs with small amounts of variables, performance will not be substantially changed, but for programs that need to manipulate large numbers of variables, performance will be affected abruptly.

Another thing, in Python 3, more precisely from version 3.6, we can display the output result in several ways. Using your code with some modifications that I think relevant, we can display the output (output) in various ways

Example 1

tempF = int(input('Temperatura em graus Farenheit: '))

tempC = (((5 * tempF) - 32) / 9)
print('Temperatura em graus Celsius: '.format(tempC))

Example 2

tempF = int(input('Temperatura em graus Farenheit: '))

tempC = (((5 * tempF) - 32) / 9)
print(f'Temperatura em graus Celsius: {tempC}')

In this case we use "f-Strings", which is nothing more than a format supported by Python interpreters of versions equal to or greater than 3.6.

Example 3

tempF = int(input('Temperatura em graus Farenheit: '))

tempC = (((5 * tempF) - 32) / 9)
print(temC)

This is another way to display the answer without much embellishment.

Example 4:

from sys import stdout

tempF = int(input('Temperatura em graus Farenheit: '))

tempC = str(((5 * tempF) - 32) / 9)
stdout.write(tempC)

Example 5º:

tempF = int(input('Temperatura em graus Farenheit: '))

tempC = ((5 * tempF - 32) / 9)
print(f'Temperatura em graus C°: {tempC:.2f}')

In this case we are formatting the output value to two decimal places

0

In the first case, you have to convert because the tempC is as int, and in the case, vc is adding strings, otherwise it would be a string + int.

In the second case you are formatting the tempC into a string, which in practice in the same:

print("Temperatura em graus Celsius: ", tempC)

or even:

print("Temperatura em graus Celsius: ", end='')
print(tempC)

Finally, I recommend you to use the type formatting:

print(f"Temperatura em graus Celsius:{tempC}")
  • I forgot that the tempC could be a float tbm, but it would be almost the same explanation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.