0

I am a beginner of Python.

I am trying to create a text file with using %s (I saw the example from textbook, but I don't know what %s exactly means here:

quizFile=open('%scapitalsquiz.txt'%2018,'w')
quizFile.write('%s. capital of %s?\n'%(1, 'New York'))
quizFile.close()

the above code will give me a text file whose name is: 2018capitalsquiz

so the %s is repalced by %2018

Also in my content of the text file, I have: 1. capital of New York?

so two %s are replaced correspondingly by 1 and New York, I just want to know what is exactly function here?

Rowling
  • 213
  • 1
  • 8
  • 20

1 Answers1

0

%s is a string formatting syntax. %s token is replaced by whatever you pass to the string after the % symbol. It basically uses the str() method to utilise the string formatting.

Nitin Prakash
  • 328
  • 3
  • 15