【问题标题】:Regarding the string format in python, what does '[%s]' mean?关于python中的字符串格式,'[%s]'是什么意思?
【发布时间】:2018-08-03 16:15:52
【问题描述】:

看到一行代码:

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

但是我不知道[%s]的作用

谁能帮帮我?

谢谢!

【问题讨论】:

标签: python string python-3.x


【解决方案1】:

它是一种字符串格式化语法(它是从 C 中借用的)。

例子:

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

这就是程序的样子:

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

您也可以通过字符串浓度来做到这一点...

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

...但是,格式化通常更容易。 您也可以将它与 多个 字符串一起使用。

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)

输出:

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.

现在,在问任何更多问题之前,请看看它们是否存在!

【讨论】:

    猜你喜欢
    • 2022-04-08
    • 1970-01-01
    • 2018-08-28
    • 2010-11-03
    • 2021-11-25
    • 2019-03-03
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    相关资源
    最近更新 更多