-6

I saw a line of code:

re.compile('[%s]' % re.escape(string.punctuation))

But I have no idea about the function of [%s]

Could anyone help me please?

Thank You!

Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
  • this is something that has been talked about before. Try looking at [this question](https://stackoverflow.com/questions/5082452/python-string-formatting-vs-format) and see if you can find what you are looking for – MattR Feb 23 '18 at 15:04
  • 5
    Possible duplicate of [What does %s mean in Python?](https://stackoverflow.com/questions/997797/what-does-s-mean-in-python) – Maxim Feb 23 '18 at 15:04
  • It means it's a really good idea to [read the official documentation](https://docs.python.org/3/library/string.html). – Jongware Feb 23 '18 at 15:04

1 Answers1

0

It is a string formatting syntax (which it borrows from C).

Example:

name = input("What is your name? ")
print("Hello %s, nice to meet you!" % name)

And this is what the program will look like:

What is your name? Johnny
Hello Johnny, nice to meet you!

You can also do this with string concentation...

name = input("What is your name? ")
print("Hello + name + ", nice to meet you!")

...However, the formatting is usually easier. You can also use this with multiple strings.

name = input("What is your name? ")
age = input("How old are you? ")
gender = ("Are you a male or a female? ")
print("Is this correct?")
print("Your name is %s, you are %s years old, and you are %s." % name, age, gender)

Output:

What is your name? Johnny
How old are you? 37
Are you a male or a female? male
Is this correct?
Your name is Johnny, you are 37 years old, and you are male.

Now, please, before asking any more questions, see if they exist!

Maxim
  • 34
  • 5