Why would I print a variable instead of just printing the string?


#1

Question

Why would I print a variable instead of just printing the string?

Answer

Often when you write a program you’ll be displaying things that change based on user input. For example, if you want to greet a user with ”Hello, username, good to see you!”, you can’t possibly know everyone’s username when writing your program. So you’d take their username as input and store it in a variable, and then you can print it later as a variable, rather than a hard-coded string!
This concept of avoiding hard-coding is generally a good practice because it allows for code that works more broadly, and should be something you actively look for when trying to improve your code.


FAQ: Learn Python – Strings & Console Output – Printing Variables