4 ms·
> Python does the same sort of weird processing... print(f"My name is {}" name) It's print(f"My name is {name}") Or print("My name is", name)
by gabrielsroka 3y ago
> Python does the same sort of weird processing...
print(f"My name is {}" name)
It's
print(f"My name is {name}")
Or
print("My name is", name)
- lproven 3y agoAfter over 20 years of trying I am still unable to cram Python into my head. I have more or less given up now. I Googled this before posting, and found this: https://www.geeksforgeeks.org/python-output-formatting/ https://www.geeksforgeeks.org/python-output-formatting/ Your comment seems to contradict that, AIUI? But TBH your specified syntax doesn't look any better to me. Why is { and } magical inside quotes? Why does putting an f before the string to be output make magic happen? Why isn't a variable called `f` output as it would be after the string?
- zozbot234 3y agoYou can think of the f" token as entering a special syntax environment where the {} characters (along with possibly other format specifiers) have that special meaning. Ultimately it's just a bit of syntactic sugar, since to do it "properly" via explicit pretty-printing and string concatenation would get rather tedious. Many varieties of BASIC have their own support for formatted output, usually via a PRINT USING statement.
- lproven 3y agoBut this is exactly the sort of thing that my point is about. The Unix/C family has the habit of inserting what looks like random line noise into its output statements, which do special stuff depending on what order they're in and where they appear relative to other line noise. Python fans think it's an easy, clear and obvious language, and as such, it's not only a suitable replacement for BASIC but a better choice. But it's not as obvious as it looks to people who grew up in the Linux world and know little else. Such as this example: `f` before the string does something totally different to `f` after the string, and there's nothing to mark or indicate that. You just have to know that and remember where it goes. > via a PRINT USING statement. For me, something like that is preferable. There is one command that does unformatted output, and a different one that formats it. That's clear. With either, what's in quotes is what comes out. If it's inside quotes, it's demarcated and it's special: the human put this in there, and don't mess with it, just do what you're told. Whereas with C, and in a different way with Python, the language does whatever it feels like with the stuff in quotes, and the human has to do extra work remembering that some stuff is magic and some isn't. I am not attempting some full language critique here... merely reporting some of my own personal experiences.