Python 字符串格式化
Python str formatting
Python 3.8.10
result = "<svg id='{id}' class='{class}' height='{height}{unit}' width='{width}{unit}'>".format(id=image_id, class=html_class, height=str(height), unit=unit_of_measure)
我收到此错误消息:
File "/home/michael/PycharmProjects/images_outer/images_project/images/templatetags/images.py", line 131
result = "<svg id='{id}' class='{class}' height='{height}{unit}' width='{width}{unit}'>".format(id=image_id, class=html_class, height=str(height), unit=str(unit_of_measure))
^
SyntaxError: invalid syntax
python-BaseException
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
你能帮帮我吗?
如果你正在使用 python 3.x 你可以使用 f strings
result = f"<svg id='{id}' class='{class}' height='{height}{unit}' width='{width}{unit}'>"
class
是 python 中的内置关键字,因此不能在此上下文中使用。代替 str.format()
使用替代的“f-string”语法来格式化字符串,例如:
result = f"<svg id='{image_id}' class='{html_class}' height='{height}{unit_of_measure}' width='{width}{unit_of_measure}'>"
Python 3.8.10
result = "<svg id='{id}' class='{class}' height='{height}{unit}' width='{width}{unit}'>".format(id=image_id, class=html_class, height=str(height), unit=unit_of_measure)
我收到此错误消息:
File "/home/michael/PycharmProjects/images_outer/images_project/images/templatetags/images.py", line 131
result = "<svg id='{id}' class='{class}' height='{height}{unit}' width='{width}{unit}'>".format(id=image_id, class=html_class, height=str(height), unit=str(unit_of_measure))
^
SyntaxError: invalid syntax
python-BaseException
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
你能帮帮我吗?
如果你正在使用 python 3.x 你可以使用 f strings
result = f"<svg id='{id}' class='{class}' height='{height}{unit}' width='{width}{unit}'>"
class
是 python 中的内置关键字,因此不能在此上下文中使用。代替 str.format()
使用替代的“f-string”语法来格式化字符串,例如:
result = f"<svg id='{image_id}' class='{html_class}' height='{height}{unit_of_measure}' width='{width}{unit_of_measure}'>"