อะไร%sหมายถึงในงูใหญ่? และโค้ดต่อไปนี้ทำอะไร?
เช่น ...
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หมายถึงในงูใหญ่? และโค้ดต่อไปนี้ทำอะไร?
เช่น ...
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ตัวยึดตำแหน่ง
แก้ไข:นี่เป็นตัวอย่างง่ายๆ:
#Python2
name = raw_input("who are you? ")
print "hello %s" % (name,)
#Python3+
name = input("who are you? ")
print("hello %s" % (name,))
%sโทเค็นช่วยให้ฉันเพื่อแทรก (และอาจจัดรูปแบบ) สตริง ขอให้สังเกตว่า%sโทเค็นจะถูกแทนที่ด้วยสิ่งที่ฉันส่งไปยังสตริงหลัง%สัญลักษณ์ โปรดสังเกตว่าฉันกำลังใช้ tuple ที่นี่เช่นกัน (เมื่อคุณมีเพียงหนึ่งสตริงที่ใช้ tuple เป็นตัวเลือก) เพื่อแสดงให้เห็นว่าสามารถแทรกและจัดรูปแบบสตริงจำนวนมากในคำสั่งเดียว
str.formatวิธีที่มีประสิทธิภาพมากกว่า
(name,)และไม่เพียงname?
คำตอบของแอนดรูว์นั้นดี
และเพื่อช่วยคุณเพิ่มอีกเล็กน้อยนี่คือวิธีที่คุณใช้การจัดรูปแบบหลายรูปแบบในสตริงเดียว
"Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".
หากคุณใช้ ints แทนสตริงให้ใช้% d แทน% s
"My name is %s and i'm %d" % ('john', 12) #My name is john and i'm 12
%sจำนวนเต็มมันก็จะถูกแปลงเป็นสตริง
print('This number will be padded with 4 zeros: %05d ' % 1)- สิ่งนี้จะได้ผล print('This number will be padded with 4 zeros: %05s ' % 1)- นี่ไม่ใช่ `
formatวิธีการได้รับการแนะนำในหลาม 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'
%sระบุประเภทการแปลงของสตริงเมื่อใช้ความสามารถในการจัดรูปแบบสตริงของงูใหญ่ โดยเฉพาะอย่างยิ่ง%sแปลงค่าที่ระบุเป็นสตริงโดยใช้str()ฟังก์ชัน เปรียบเทียบสิ่งนี้กับ%rประเภทการแปลงที่ใช้repr()ฟังก์ชันสำหรับการแปลงค่า
%sและ%dเป็นตัวระบุรูปแบบหรือตัวยึดตำแหน่งสำหรับการจัดรูปแบบสตริง / ทศนิยม / ลอย ฯลฯ
ตัวระบุรูปแบบที่ใช้บ่อยที่สุดใน MOST :
%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
#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 help of older method
print ('{} {}\'s age is {} with incredible IQ of {} '.format(name, extendedName, age, IQ)) # with help of 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 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!"
เพื่อตอบคำถามที่สองของคุณ: รหัสนี้ทำอะไร? ...
นี่เป็นรหัสตรวจสอบข้อผิดพลาดมาตรฐานสำหรับสคริปต์ Python ที่ยอมรับอาร์กิวเมนต์บรรทัดคำสั่ง
ดังนั้นข้อความแรกifแปลเป็น: หากคุณยังไม่ผ่านการโต้แย้งฉันจะบอกคุณว่าคุณควรผ่านการโต้แย้งฉันในอนาคตเช่นคุณจะเห็นบนหน้าจอนี้:
Usage: myscript.py database-name
ifคำสั่งถัดไปจะตรวจสอบว่า 'ฐานข้อมูลชื่อ' ที่คุณส่งไปยังสคริปต์นั้นมีอยู่จริงในระบบไฟล์หรือไม่ ถ้าไม่คุณจะได้รับข้อความเช่นนี้:
ERROR: Database database-name was not found!
จากเอกสาร :
argv [0] เป็นชื่อสคริปต์ (ขึ้นอยู่กับระบบปฏิบัติการไม่ว่าจะเป็นชื่อพา ธ แบบเต็มหรือไม่ก็ตาม) หากคำสั่งถูกเรียกใช้งานโดยใช้ตัวเลือกบรรทัดคำสั่ง -c ให้กับล่าม argv [0] จะถูกตั้งค่าเป็นสตริง '-c' หากไม่มีการส่งชื่อสคริปต์ไปยังตัวแปล Python argv [0] เป็นสตริงว่าง
นี่เป็นตัวอย่างที่ดีใน 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
%ผู้ประกอบการจะเลิกในความโปรดปรานของมีประสิทธิภาพมากขึ้นstr.formatวิธีการดูPEP-3101