#๐Ÿ”’ is it possible to make formats like 'f' strings?

41 messages ยท Page 1 of 1 (latest)

tribal bluff
#

if yes how?

brazen forumBOT
#

@tribal bluff

Python help channel opened

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.

opaque anchor
lime gale
#

u mean like

f"text {code}"```
#

!e

x = 5
y = 6
print(f"{x} times {y} is {x*y}")```
brazen forumBOT
tribal bluff
dusty fable
#

yeah, you can make your own string format and use regex to substitute stuff in it

lime gale
tribal bluff
lime gale
#

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
tribal bluff
lime gale
#

there is r"" which means raw text

dusty fable
#

!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)

brazen forumBOT
tribal bluff
dusty fable
#

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

lime gale
tribal bluff
#

ohh :( so i cant actually make my own format

dusty fable
#

didn't i demonstrate that you can make your own format?

lime gale
dusty fable
#

i literally just invented a string format for you

#

"some text %VARIABLE% some text"

tribal bluff
lime gale
#

u can't do that

dusty fable
#

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

lime gale
#

your asking to change python's syntax

#

can't

tribal bluff
lime gale
#

can't

tribal bluff
# lime gale can't

now i know im just waiting inuk to say his last words and im closeing this thread

lime gale
#

still impossible

dusty fable
#

!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)```
brazen forumBOT
tribal bluff
#

!solved

brazen forumBOT
#
Python help channel closed

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.