#๐ is it possible to make formats like 'f' strings?
41 messages ยท Page 1 of 1 (latest)
@tribal bluff
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
What do you mean?
:white_check_mark: Your 3.12 eval job has completed with return code 0.
5 times 6 is 30
yes just instead of f define one myself
yeah, you can make your own string format and use regex to substitute stuff in it
what are you trying to do
yust experimenting playing around
how?
u can't make your own f string. it would be simpler to just create a function that formats strings and call it
def my_format(st):
return formatted_st
i seen something like re"" and as far as i know it was an inport
there is r"" which means raw text
!e py import re VARS = { "WORLD": "Earth", } PAT = re.compile(r"%(.*?)%") S = "Hello %WORLD%!" out = PAT.sub(lambda m: VARS[m[1]], S) print(out)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
Hello Earth!
oh its r"" there but how does that work?
the r"" in this case doesn't do anything, it's just good style when you're making regexes
r"%(.*?)%" == "%(.*?)%"
it's good style because often you need to put a literal backslash \ inside a regex, and r"" makes sure the backslash isn't treated specially by python
it doesn't do anything it takes the text as raw text
ohh :( so i cant actually make my own format
didn't i demonstrate that you can make your own format?
u can like this
i can but not like i imagined i imagined 'f' is defined somewhere and i can define stuff like that like i can make decorators and ect
u can't do that
ah, f"" is firmly a part of python syntax, you can not define your own string literal prefixes
there is
u""
r""
f""
and that's it
i just thought i can do it kindof like decorators nevermind
can't
now i know im just waiting inuk to say his last words and im closeing this thread
still impossible
!e you can, however, define your own string formatting options within an f-string (but this requires a custom class that understands it):
class CoolClass:
def __init__(self, var):
self.var = var
def __format__(self, fmt):
if fmt == "reversed":
return str(self.var)[::-1]
return str(self.var)
a = CoolClass("Hello")
b = CoolClass("Earth")
s = f"{a} {b:reversed}!"
print(s)```
:white_check_mark: Your 3.12 eval job has completed with return code 0.
Hello htraE!
thats actually cool im gonna play with that until my friend send me the code i waanted to finish thx
!solved
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.