开发者

Why do Ruby and Python have the % for formatting instead of "format"

开发者 https://www.devze.com 2023-03-10 06:45 出处:网络
I mean what makes % look so much attractive compared to good old 开发者_运维问答.format?! I know this may be subjective, but I wonder if there is something about this. They inherit them from C. Speci

I mean what makes % look so much attractive compared to good old 开发者_运维问答.format?!

I know this may be subjective, but I wonder if there is something about this.


They inherit them from C. Specifically, sprintf.


I'd assume they use % because that's what the old C printf format strings looked like.


It's just a piece of syntactic sugar, meant to make a common operation more terse. If you don't like it, you can use str.format() instead. I enjoy it because it makes printf()-style formatting stand out in the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消