Reading code and %s %d
Today’s Learn Python the Hard Way lesson taught me an easy way to become more fluent with Python or any programming language. The lesson instructs you to type in a short program and then to write a comment for each line of code. The comments explain what each line does. I like this because you need to read each line of code and determine what it does.
Certainly, the lesson’s example was simple. However, reading code is an important skill. I’ve sat in some programming interviews when I worked at a startup. Many of the college grads brought examples of programs they wrote. However, they could not read a program we gave them and to tell us what the program did. Debugging was another concern. These are skills that I don’t believe CompSci teaches. I may be wrong. Please let me know.
Programming languages are specific. They can define a distinct character or group of characters to represent something in a particular context. Format strings are one case.
In a previous post, I mentioned that you could use the print function and the comma to mix text with arithmetic operations and/or variables.
You can create the same message using format strings.
The format string %s represents a string (text) variable and the %d format string represents a numeric variable.
Our example contained two variables, so we enclose them in parentheses. A single variable just needs to be preceded by a % sign.