python-discord / meta

Issue tracker for suggestions and other questions relating to our community
https://pythondiscord.com
30 stars 5 forks source link

Add a tag for the String Formatting Mini Language #143

Closed janine9vn closed 3 years ago

janine9vn commented 3 years ago

Tag Name

!string-formatting

What kind of content should the tag include?

The Python String Formatting Mini Language is a powerful tool once people discover f-string. You can see the specification here: https://docs.python.org/3/library/string.html#format-specification-mini-language I would also like to include this resource and it breaks it down in a very readable manner: https://pyformat.info/

Ideally, this tag would showcase some of the more obviously useful ones and then link to the full spec. The ones I think should be included:

my_num = 2134234523
print(f"{my_num:,}")  # Will add in commas where appropriate

my_smaller_num = -30.0532234
print(f"{my_smaller_num:=09.2f}")  # Will take up 9 total character widths and display the sign

final_grade = 0.935
print(f"{final_grade:.2%}")  # Will convert it to the appropriate percentage with precision

my_str = "Center me!"
print(f"{my_str:^20}")  # Will center my_str with a total character width of 20

Results:

2,134,234,523
-00030.05
93.50%
      Center me!
doublevcodes commented 3 years ago

Hey, I agree that this should be a tag and you've done a nice job of showcasing a few major features. 👍. I also think the !r modifier which shows the __repr__ of an object should be featured.

janine9vn commented 3 years ago

So, for showcasing the repr I think this will work:

repr_str = "Spam \n Ham"
print(f"{repr_str!r}")  # You can easily call repr on objects within an f-string

Results:

'Spam \n Ham'

If we're good with this, I can work to get this tag written this weekend

janine9vn commented 3 years ago

This is now a thing! View the tag by running this in bot-commands: !string-formatting