zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[Python] String Formatting

Python string
2023-09-14 09:00:51 时间

One particularly useful string method is format. The format method is used to construct strings by inserting values into template strings. Consider this example for generating log messages for a hypothetical web server.

log_message = "IP address {} accessed {} at {}".format(user_ip, url, now)

If the variables user_ipurl and now are defined then they will be substituted for the {} placeholder values:

> print(log_message)
IP address 208.94.117.90 accessed /bears/koala at 16:20