คุณจะใช้ชื่อตัวแปรภายในตัวระบุรูปแบบ Python ได้อย่างไร

เป็นไปได้ไหมที่จะใช้ตัวแปรภายในตัวระบุการจัดรูปแบบสตริง Python

ฉันเหนื่อย:

display_width = 50
print('\n{:^display_width}'.format('some text here'))

แต่ได้ ValueError: Invalid format specifier ฉันได้ลองแล้ว display_width = str(50)

อย่างไรก็ตาม การป้อน print('\n{:^50}'.format('some text here')) ก็ใช้ได้ดี


person Christopher Pearson    schedule 14.03.2015    source แหล่งที่มา


คำตอบ (4)


ใช่ แต่คุณต้องส่งต่อเป็นอาร์กิวเมนต์ให้กับ format จากนั้นอ้างถึงพวกมันใน {} เช่นเดียวกับที่คุณทำกับชื่ออาร์กิวเมนต์:

print('\n{:^{display_width}}'.format('some text here', display_width=display_width))

หรือสั้นกว่าแต่ชัดเจนน้อยกว่าเล็กน้อย:

print('\n{:^{}}'.format('some text here', display_width))

เนื่องจากคำถามนี้ถูกโพสต์ครั้งแรก Python 3.6 ได้เพิ่ม f-strings ซึ่งช่วยให้คุณทำได้โดยไม่ต้องใช้ format วิธีการและใช้ตัวแปรที่อยู่ในขอบเขตแทนที่จะต้องส่งผ่านตัวแปรที่มีชื่อเป็นอาร์กิวเมนต์ของคำหลัก:

display_width = 50
text = 'some text here'
print(f'\n{text:^{display_width}}')
person Brian Campbell    schedule 14.03.2015
comment
ใช่แล้ว แค่นั้นแหละ! จริงๆ แล้ว ฉันใช้ตัวเลือกที่สองของคุณ แต่ได้เพิ่มตัวระบุเพื่อให้ชัดเจนยิ่งขึ้น: print('\n{0:^{1}}'.format('some text here', display_width)) สิ่งนี้ทำให้ฉันมีความคิดสร้างสรรค์มากขึ้นอีกเล็กน้อยเหมือนใน for loop: print(('{0:>{2}} : {1:<}').format(k, v, width_adj)) โดยที่ width_adj = display_width//2 - 2 - person Christopher Pearson; 14.03.2015
comment
@ChristopherPearson ใช่นั่นดูเหมือนเป็นวิธีที่สมเหตุสมผลที่จะทำ - person Brian Campbell; 14.03.2015
comment
ตามหมายเหตุที่ python.org/dev/peps/pep-3101 กล่าวถึง ^ มีไว้สำหรับจัดกึ่งกลาง > มีไว้สำหรับจัดชิดขวา และ < มีไว้สำหรับจัดชิดซ้าย - person depperm; 01.12.2016
comment
มีวิธีในการทำเช่นนั้นโดยใช้สำนวนสตริงที่จัดรูปแบบ 3.6 f"..." หรือไม่ - person steffen; 10.09.2017
comment
ตัวเลือกแรกใช้งานไม่ได้สำหรับฉันใน 3.6.1 แต่เวอร์ชันที่สอง (ตัวแปรเป็น arg to format) ทำงานได้ดี นี่เป็นพฤติกรรมที่คาดหวังสำหรับ 3.6 หรือไม่ - person Omortis; 09.10.2017
comment
@Omortis ตัวเลือกแรกทำงานได้ดีสำหรับฉันใน Python 3.6 display_width = 50; print('\n{:^{display_width}}'.format('some text here', display_width=display_width)). คุณสามารถแสดงรหัสที่ใช้ไม่ได้กับคุณได้หรือไม่? - person Brian Campbell; 11.10.2017
comment
@steffen คำตอบคือใช่ ดู stackoverflow.com/a/60838497/5693642 - person Chang Ye; 25.03.2020
comment
@steffen ได้อัปเดตคำตอบของฉันเพื่อรวม f-strings - person Brian Campbell; 25.03.2020

Python f-string มีความยืดหยุ่นมากกว่า

>>> display_width = 50
>>> display_content = "some text here"
>>> print(f'\n{display_content:^{display_width}}')

                  some text here
person Chang Ye    schedule 24.03.2020

ดูเหมือนว่าตั้งแต่คำถามนี้ถูกโพสต์ครั้งแรก python ได้แนะนำ f-strings ดูหน้าเว็บนี้สำหรับข้อมูล

>>> name = 'Fred'
>>> age = 42
>>> f'He said his name is {name} and he is {age} years old.'
He said his name is Fred and he is 42 years old.
person Bolton Bailey    schedule 09.02.2020

อาจจะ

print(('{0:^'+str(display_width)+'}').format('hello'))
person shooper    schedule 14.03.2015