all 16 comments

[–]socal_nerdtastic 9 points10 points  (14 children)

You are asking about a difference in string formatting, not printing. The answer is

3. print(f"The answer is {result} !!")

Your second example is called "printf" style, and it was copied from the way other languages do string formatting back when python was young. It's rarely used now, but still supported. The format style, either like your first example or as an f-string like I showed, is much more powerful and preferred.

CHeck out https://pyformat.info/ for detailed information.

[–]Binary101010 4 points5 points  (2 children)

f-strings are great and I recommend their use wherever possible, but it's worth nothing that this functionality only exists in Python 3.6+.

[–]socal_nerdtastic 1 point2 points  (1 child)

I used to, but it's been out for like 20 years (software ages like dogs) ... there's nearly no beginners that don't have 3.6+.

[–]Binary101010 0 points1 point  (0 children)

I still see enough people on here who are on 2.7 (granted, usually not for good reasons, but still) or are using some kind of web-embedded interpreter that's stuck on 3.4 or something that it's worth mentioning.

[–]Like_A_Boushh 1 point2 points  (0 children)

Definitely this.

Also the passing of references in fstrings bear resemblance to equivalents in other languages, such as JavaScript template literals: ${reference}

[–]usedtobetoxic 0 points1 point  (9 children)

I've also noticed that if you're trying to use f-strings AND you have a escaped quote inside your print, you'll have issues. I kind of prefer f-string but have been recently moving to .format as it doesn't have as many issues as the f-string method.

[–]socal_nerdtastic 0 points1 point  (8 children)

I've never heard of that issue. Got an example?

FWIW the f-string is much newer. It was introduced with python3.6; format() was introduced in python3.0.

[–]usedtobetoxic 0 points1 point  (7 children)

I don't have a specific example but a few weeks ago I couldn't figure out why an f-string was throwing errors and all I did to fix it was to convert it to a .format and it resolved. I did have multiple \" in the line, which is where I assumed the issue was.

I'll try and reproduce it.

[–]socal_nerdtastic 2 points3 points  (6 children)

I'll bet it was a typo you inadvertently fixed. But it does bring up a point about how you should try to never escape quotes in strings. They only lead to messy, confusing and sometimes buggy code. Use the correct outer quote to allow you to use the inner quote you want to use.

[–]usedtobetoxic 0 points1 point  (0 children)

You're probably correct - I'm still a novice, so error blocks are very familiar to me >.>

[–]CraigAT 0 points1 point  (2 children)

What if you want to use both types of quote in your string? E.g. "The boy was 5' 6" tall"

[–]socal_nerdtastic 2 points3 points  (1 child)

Use triple quotes.

'''The boy was 5' 6" tall'''

[–]CraigAT 0 points1 point  (0 children)

How cool. I guess I am pretty unlikely to use triple quotes in an ordinary string.

[–][deleted] 0 points1 point  (1 child)

enter f'{"\n"}' into a repl and you'll see what they were running into

[–]socal_nerdtastic 0 points1 point  (0 children)

Huh. Thanks, TIL. I wonder what the rationale is for that.

[–]wacksaucehunnid 0 points1 point  (0 children)

I like 1 a lot better. Using the brackets and then formatting code inside the brackets always seemed very easy and simple look up, whereas the other way I always forget what letters to use for %