1

I am currently working on a "Learn Python" course, and I came across this snippet:

f.write("This is line %d\r\n" % (i+1))

The thing is, I'm not entirely sure what "%d\r\n" and "%" are doing in the string?

Could someone tell me what they're doing, or point me in the right direction to some resources?

Thank you.

user3840170
  • 26,597
  • 4
  • 30
  • 62
dav1d
  • 21
  • 1
  • 1
    https://docs.python.org/3/tutorial/inputoutput.html – 001 Feb 22 '22 at 16:11
  • 4
    This is string-formatting taking place in a string that contains a couple escaped characters. `\r` is the carriage return character, `\n` is the newline character. `%d` is string-formatting an integer, which you supply via `% (i+1)`. – Paul M. Feb 22 '22 at 16:11
  • 2
    If the course does not explain the code it uses, consider finding a better course – DeepSpace Feb 22 '22 at 16:11
  • +1 on what DeepSpace said, and from that line alone I can determine this is an older course. Try finding a more recent one – Edo Akse Feb 22 '22 at 16:14

0 Answers0