What does %s mean in Python? And what does the following bit of code do?
在Python中,%s是什么意思?下面这段代码的作用是什么?
For instance...
例如……
if len(sys.argv) < 2:
sys.exit('Usage: %s database-name' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Database %s was not found!' % sys.argv[1])
6 个解决方案
#1
149
It is a string formatting syntax (which it borrows from C).
它是一个字符串格式语法(它从C中借用)。
Please see "Formatting Strings":
请参阅“格式化字符串”:
Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the
%s
placeholder.Python支持将值格式化为字符串。尽管这可能包含非常复杂的表达式,但最基本的用法是将值插入到带有%s占位符的字符串中。
Edit: Here is a really simple example:
编辑:这里有一个非常简单的例子:
name = raw_input("who are you?")
print "hello %s" % (name,)
The %s
token allows me to insert (and potentially format) a string. Notice that the %s
token is replaced by whatever I pass to the string after the %
symbol. Notice also that I am using a tuple here as well (when you only have one string using a tuple is optional) to illustrate that multiple strings can be inserted and formatted in one statement.
%s令牌允许我插入(并可能格式化)一个字符串。注意,%s标记被我在%符号之后传递给字符串的任何东西所替换。还要注意,这里我也在使用tuple(当您只有一个字符串时,使用tuple是可选的)来说明可以在一条语句中插入和格式化多个字符串。
#2
79
Andrew's answer is good.
安德鲁的回答是好的。
And just to help you out a bit more, here's how you use multiple formatting in one string
为了帮助大家更好地理解,这里是如何在一个字符串中使用多重格式的
"Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".
If you are using ints instead of string, use %d instead of %s.
如果您使用ints代替string,请使用%d代替%s。
"My name is %s and i'm %d" % ('john', 12) #My name is john and i'm 12
#3
18
The format
method was introduced in Python 2.6. It is more capable and not much more difficult to use:
在Python 2.6中引入了格式方法。它更有能力,使用起来也不难:
>>> "Hello {}, my name is {}".format('john', 'mike')
'Hello john, my name is mike'.
>>> "{1}, {0}".format('world', 'Hello')
'Hello, world'
>>> "{greeting}, {}".format('world', greeting='Hello')
'Hello, world'
#4
11
%s
indicates a conversion type of string when using python's string formatting capabilities. More specifically, %s
converts a specified value to a string using the str()
function. Compare this with the %r
conversion type that uses the repr()
function for value conversion.
%s表示使用python的字符串格式化功能时的字符串转换类型。更具体地说,%s使用str()函数将指定值转换为字符串。与使用repr()函数进行值转换的%r转换类型进行比较。
Take a look at the docs for string formatting.
看看文档中的字符串格式。
#5
6
In answer to your second question: What does this code do?...
回答你的第二个问题:这个代码是做什么的?
This is fairly standard error-checking code for a Python script that accepts command-line arguments.
对于接受命令行参数的Python脚本,这是相当标准的错误检查代码。
So the first if
statement translates to: if you haven't passed me an argument, I'm going to tell you how you should pass me an argument in the future, e.g. you'll see this on-screen:
第一个if语句的意思是:如果你还没有向我提出一个论点,我将告诉你以后该如何向我提出一个论点,例如:你会在屏幕上看到这个:
Usage: myscript.py database-name
The next if
statement checks to see if the 'database-name' you passed to the script actually exists on the filesystem. If not, you'll get a message like this:
下一个if语句检查传递给脚本的“database-name”是否在文件系统中实际存在。如果没有,你会得到这样的信息:
ERROR: Database database-name was not found!
From the documentation:
从文档:
argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.
argv[0]是脚本名称(它取决于是否为完整路径名)。如果使用解释器的-c命令行选项执行命令,那么argv[0]将设置为字符串'-c'。如果没有将脚本名传递给Python解释器,那么argv[0]就是空字符串。
#6
0
Here is a good example in Python3.
下面是Python3中的一个很好的例子。
>>> a = input("What is your name?")
What is your name?Peter
>>> b = input("Where are you from?")
Where are you from?DE
>>> print("So you are %s of %s" % (a, b))
So you are Peter of DE
#1
149
It is a string formatting syntax (which it borrows from C).
它是一个字符串格式语法(它从C中借用)。
Please see "Formatting Strings":
请参阅“格式化字符串”:
Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the
%s
placeholder.Python支持将值格式化为字符串。尽管这可能包含非常复杂的表达式,但最基本的用法是将值插入到带有%s占位符的字符串中。
Edit: Here is a really simple example:
编辑:这里有一个非常简单的例子:
name = raw_input("who are you?")
print "hello %s" % (name,)
The %s
token allows me to insert (and potentially format) a string. Notice that the %s
token is replaced by whatever I pass to the string after the %
symbol. Notice also that I am using a tuple here as well (when you only have one string using a tuple is optional) to illustrate that multiple strings can be inserted and formatted in one statement.
%s令牌允许我插入(并可能格式化)一个字符串。注意,%s标记被我在%符号之后传递给字符串的任何东西所替换。还要注意,这里我也在使用tuple(当您只有一个字符串时,使用tuple是可选的)来说明可以在一条语句中插入和格式化多个字符串。
#2
79
Andrew's answer is good.
安德鲁的回答是好的。
And just to help you out a bit more, here's how you use multiple formatting in one string
为了帮助大家更好地理解,这里是如何在一个字符串中使用多重格式的
"Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".
If you are using ints instead of string, use %d instead of %s.
如果您使用ints代替string,请使用%d代替%s。
"My name is %s and i'm %d" % ('john', 12) #My name is john and i'm 12
#3
18
The format
method was introduced in Python 2.6. It is more capable and not much more difficult to use:
在Python 2.6中引入了格式方法。它更有能力,使用起来也不难:
>>> "Hello {}, my name is {}".format('john', 'mike')
'Hello john, my name is mike'.
>>> "{1}, {0}".format('world', 'Hello')
'Hello, world'
>>> "{greeting}, {}".format('world', greeting='Hello')
'Hello, world'
#4
11
%s
indicates a conversion type of string when using python's string formatting capabilities. More specifically, %s
converts a specified value to a string using the str()
function. Compare this with the %r
conversion type that uses the repr()
function for value conversion.
%s表示使用python的字符串格式化功能时的字符串转换类型。更具体地说,%s使用str()函数将指定值转换为字符串。与使用repr()函数进行值转换的%r转换类型进行比较。
Take a look at the docs for string formatting.
看看文档中的字符串格式。
#5
6
In answer to your second question: What does this code do?...
回答你的第二个问题:这个代码是做什么的?
This is fairly standard error-checking code for a Python script that accepts command-line arguments.
对于接受命令行参数的Python脚本,这是相当标准的错误检查代码。
So the first if
statement translates to: if you haven't passed me an argument, I'm going to tell you how you should pass me an argument in the future, e.g. you'll see this on-screen:
第一个if语句的意思是:如果你还没有向我提出一个论点,我将告诉你以后该如何向我提出一个论点,例如:你会在屏幕上看到这个:
Usage: myscript.py database-name
The next if
statement checks to see if the 'database-name' you passed to the script actually exists on the filesystem. If not, you'll get a message like this:
下一个if语句检查传递给脚本的“database-name”是否在文件系统中实际存在。如果没有,你会得到这样的信息:
ERROR: Database database-name was not found!
From the documentation:
从文档:
argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.
argv[0]是脚本名称(它取决于是否为完整路径名)。如果使用解释器的-c命令行选项执行命令,那么argv[0]将设置为字符串'-c'。如果没有将脚本名传递给Python解释器,那么argv[0]就是空字符串。
#6
0
Here is a good example in Python3.
下面是Python3中的一个很好的例子。
>>> a = input("What is your name?")
What is your name?Peter
>>> b = input("Where are you from?")
Where are you from?DE
>>> print("So you are %s of %s" % (a, b))
So you are Peter of DE