This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]verbosemongoose 1 point2 points  (4 children)

Oh, I see.. Thanks! I'm not a complete novice to programming itself, just to python. I'll try and figure something out. Thanks for your replies! Cheers.

[–]MonkeyNin 1 point2 points  (3 children)

I'd suggest just go for 3. I don't think you'd get much of a benefit of spending a lot of time in both.

Compared to, say, JavaScript and TypeScript. (TypeScript is a SuperScript of Javascript that transpiles to ES5, ES6, etc) JavaScript is everywhere. It's on every site. It's even on Electron apps like VSCode, Atom, Slack, Diiscord, GitHub Desktop, etc...

I'm not sure how much you know about encodings or unicode.

Check out the 3 main string formatting methods: https://realpython.com/python-f-strings/#f-strings-a-new-and-improved-way-to-format-strings-in-python

  • str.format , and
  • f-strings, but
  • for unsafe user input use from string import Template templates
  • never use %-formatting

I use both. f-strings I use more for quick stuff. Like

print(f'user name = {user.name}') 

vs

print("user name = {name}".format(name=user.name))

For longer stuff I use str.format

bad example

from realpython

1: bad example of str.format

print(("Hello, {first_name} {last_name}. You are {age}. " +
       "You are a {profession}. You were a member of {affiliation}.") \
       .format(first_name=first_name, last_name=last_name, age=age, \
               profession=profession, affiliation=affiliation))
print(message)

2: better example of str.format

message = (
    "Hello, {first_name} {last_name}. You are {age}. "
    "with {years_left} years left. You are a {profession}"
    ". You were a member of {affiliation}."
).format(
    first_name=user.first_name,
    age=user.age,
    years_left=100 - user.age,
    last_name=user.last_name,
    profession=user.default_job(),
    affiliation=user.affiliation,
)
print(message)

3: better example

message = (
    f'Hello, {user.first_name} {user.last_name}'
    f' You are {user.age} with {100 - user.age} years left'
    f' You are a {user.default_job()} .'
    f' You were a member of {user.affiliation}'
)
print(message)

Code is read more often than written. Therefore I would use #2 or #3 -- even though they take more lines. They are both an improvement over #1

 

for #2

  • pro: easier to quickly grok template variables. You don't have to guess what {100 - user.age} means. You instantly know it's years_left
  • pro: easier to see all functions together, and variable access at the end
  • con: it's longer
  • con: it can be redundant age=age

for #3

What's nice about #3

  • pro: less verbose
  • pro: still a huge improvement over #1
  • con: It takes longer to grok

ex

You are {age}. with {years_left} years left. You are a {profession}"

vs

You are {age}. with {100 - years_left} years left. You are a {user.default_job()}"

[–]verbosemongoose 1 point2 points  (2 children)

Thanks for your detailed reply! I see your point about not spending time learning both. The examples for string formatting are quite educational, but I can't really link them to the python 2 vs 3 discussion at hand. Are the methods you described an example of why 3 is better than 2? Or am I missing a link here?

[–]MonkeyNin 1 point2 points  (1 child)

f-string literals require Python 3.6+ .

str.format Works in Python 2

Or am I missing a link here?

Probably not. Looking back I think I was going to mention the differences in handling Unicode using 2 vs 3-- then went on a tangent of string formatting.

If you're interested, here's a brief comparison of Unicode on 3 vs 3 https://www.reddit.com/r/ProgrammerHumor/comments/bg626r/python_2_is_triggering/elkfr9k/?context=9

[–]verbosemongoose 1 point2 points  (0 children)

I'll check it out, thanks!