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

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?

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 :

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.

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