pythonprintingconceptual

Printing int and float in Python


Considering the example below, why would anyone choose the first print instead of the second?

def main():
    name = input("Input your name:")
    age = int(input("Input you age:"))

    print("Your name is " + name + " and you are " + str(age) + " years old.")
    print("Your name is", name, "and you are" , age, "years old.")

if __name__ == "__main__":
    main()

It seems to me that print("text", var, "text") is more straightforward, since there's no need to explicitly convert our int to str.

Are there any relevant use cases for the print("text"+str(var)+"text") format?


Solution

  • The usual way of doing this is formatting like:

    print(f"Your name is {name} and you are {age} years old.")
    

    Considering the example below, why would anyone choose the first print instead of the second?

    Well, the first way (concatenation of strings and expressions converted to strings) is obsolete and unusable for me, since we have proper formatting (like my example above). We can construct any string we want straightforwardly and clearly.

    If we apply the same logic to *args approach, as for me, the outcome is the same.