%s在Python中是什么意思?下面这段代码做什么呢?
例如……
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])
%s在Python中是什么意思?下面这段代码做什么呢?
例如……
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])
它是一种字符串格式语法(从C中借来的)。
请参阅“PyFormat”:
Python支持将值格式化为 字符串。尽管这可以包括 非常复杂的表达式 的基本用法是将值插入到 带有%s占位符的字符串。
这里有一个非常简单的例子:
#Python 2
name = raw_input("who are you? ")
print "hello %s" % (name,)
#Python 3+
name = input("who are you? ")
print("hello %s" % (name,))
%s令牌允许我插入(并可能格式化)一个字符串。注意,%s标记被我传递给字符串%符号后面的任何符号所取代。还要注意,我在这里也使用了一个元组(当您只有一个字符串时,使用元组是可选的)来说明可以在一条语句中插入和格式化多个字符串。
%s表示使用Python的字符串格式化功能时字符串的转换类型。更具体地说,%s使用str()函数将指定的值转换为字符串。将其与使用repr()函数进行值转换的%r转换类型进行比较。
看一看字符串格式化的文档。
回答你的第二个问题:这段代码做什么?
这是接受命令行参数的Python脚本的相当标准的错误检查代码。
所以第一个if语句转换为:如果你还没有传递给我一个参数,我将告诉你将来应该如何传递给我一个参数,例如,你将在屏幕上看到:
Usage: myscript.py database-name
下一个if语句检查您传递给脚本的'database-name'是否确实存在于文件系统中。如果没有,你会得到这样的消息:
错误:数据库数据库名不存在!
从文档中可以看到:
Argv[0]是脚本名称(它是 依赖于操作系统 这是否是一个完整的路径名)。如果 使用-c命令执行该命令 属性的命令行选项 解释器,argv[0]被设置为 字符串“c”。如果没有脚本名称为 传递给Python解释器, Argv[0]是空字符串。
安德鲁的回答很好。
为了帮助你,这里是你如何在一个字符串中使用多种格式:
"Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".
如果使用int型而不是字符串,请使用%d而不是%s。
"My name is %s and I'm %d" % ('john', 12) #My name is john and I'm 12
format方法在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'
>>> '%s' % name
"{'s1': 'hello', 's2': 'sibal'}"
>>> '%s' %name['s1']
'hello'
这是Python 3中的一个很好的例子。
>>> 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.
%和%d是格式说明符或占位符,用于格式化字符串、小数、浮点数等。
最常用的格式说明符:
% s:字符串
% d:小数
% f:浮动
自解释代码:
name = "Gandalf"
extendedName = "the Grey"
age = 84
IQ = 149.9
print('type(name): ', type(name)) # type(name): <class 'str'>
print('type(age): ', type(age)) # type(age): <class 'int'>
print('type(IQ): ', type(IQ)) # type(IQ): <class 'float'>
print('%s %s\'s age is %d with incredible IQ of %f ' %(name, extendedName, age, IQ)) # Gandalf the Grey's age is 84 with incredible IQ of 149.900000
# The same output can be printed in following ways:
print ('{0} {1}\'s age is {2} with incredible IQ of {3} '.format(name, extendedName, age, IQ)) # With the help of an older method
print ('{} {}\'s age is {} with incredible IQ of {} '.format(name, extendedName, age, IQ)) # With the help of an older method
print("Multiplication of %d and %f is %f" %(age, IQ, age*IQ)) # Multiplication of 84 and 149.900000 is 12591.600000
# Storing formattings in a string
sub1 = "python string!"
sub2 = "an arg"
a = "I am a %s" % sub1
b = "I am a {0}".format(sub1)
c = "with %(kwarg)s!" % {'kwarg':sub2}
d = "with {kwarg}!".format(kwarg=sub2)
print(a) # "I am a python string!"
print(b) # "I am a python string!"
print(c) # "with an arg!"
print(d) # "with an arg!"