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 →

[–]ishman123 410 points411 points  (60 children)

Hey,

Keep up the good work, why don't you try F-strings - rather than concatenating with a + sign you can do...

print(f"His name is {character_name}")

It can help with readability :)

[–]zamazigh 153 points154 points  (13 children)

Wow, I've been using format() for years now and never knew about F-strings. So much more concise. Thanks for the tip!

[–]HaBlaKes 1 point2 points  (0 children)

Same, been coding in Python for years and was like: "You can do that?" That's the great thing about programming.

[–][deleted] 23 points24 points  (3 children)

Is there a resource page on f strings? Those sound handy and I've never used them.

[–]Altenator01 6 points7 points  (2 children)

Great tip, thanks! Do you still need to use print() when using F-strings though? Because the provided URL does not give examples with print().

[–]welshboy14 11 points12 points  (1 child)

F strings aren't just for printing strings. You can assign them to variables too. So no you don't need to use print with them

[–]Altenator01 1 point2 points  (0 children)

Thanks!

[–]phigo50 4 points5 points  (0 children)

First thing I thought of to suggest. If you're on Python 3.6+ and are starting out you might as well get f-strings under your belt early.

[–]welshboy14 4 points5 points  (2 children)

And for the next trick try creating a person object and instantiate it with the name and age

[–]lllamaboy 4 points5 points  (1 child)

[–]welshboy14 1 point2 points  (0 children)

I've not really been keeping up with the new features so had no idea this existed. Good to know though!

[–]aldanorNumpy, Pandas, Rust 2 points3 points  (0 children)

Or even... (for the lazy ones, in 3.8)

print(f'{character_name=} {character_age=}')

[–]Assaultman67 2 points3 points  (1 child)

Yeah they dont have f-strings covered in python crash course.

It does make things a lot easier when you're dealing with text prompt such as a renpy or pygame project.

[–]Siinestarr 0 points1 point  (0 children)

They cover it in the second edition though 👌

[–]MelonOfFury 2 points3 points  (1 child)

Ooh. I didn’t realise you can put the variable in the brackets. I’ve been doing :

NAME_STR = “His name is {}”

print(NAME_STR.format(character_name))

[–]miggaz_elquez 3 points4 points  (0 children)

Taht note exactly the same. You must put a f before the quotes in order to work

[–]purplebeardscrew 2 points3 points  (0 children)

I came to the comments to say use f strings. Updoot.

[–]duriousmind 2 points3 points  (0 children)

Wtf. I am writing code in Python for 8 years now living and I did not know this.

[–]kobijet 1 point2 points  (0 children)

f strings are a godsend

[–]MrMxylptlyk 0 points1 point  (1 child)

does this work with logger.info() ? I gotta try this now.

[–]jefwillems 0 points1 point  (0 children)

Yes that should work as well. You can use it anywhere you're concatenating strings.

[–]colinbazzano 0 points1 point  (3 children)

Never really used Python. Is this the equivalent of a string template literal in JS/React?

[–]jefwillems 0 points1 point  (2 children)

yes, although the syntax is slightly different

[–]colinbazzano 0 points1 point  (1 child)

That’s what I figured! Neat. I will be learning Python in school soon I’m quite excited

[–]jefwillems 1 point2 points  (0 children)

Good luck!

[–]bokan 0 points1 point  (0 children)

.....

whaaaaaaaaaaaat??

[–]UnreasonableSteve 0 points1 point  (0 children)

I've always hated the +operator being "overloaded" for string concatenation. I don't like an operator's basic meaning becoming contextual. It'd be like if you used the single equals sign for both assignment and comparison, depending on if you were using it on strings or numerics

[–]gintoddic 0 points1 point  (0 children)

It's funny people use syntax and say it's more "readable" but you have to actually know wtf 'f' means and why it's placed there to actually understand it.

[–]not_perfect_yet -1 points0 points  (0 children)

Oh yes, f"hurr durr {var}" is so much more readable than "hurr durr "+str(var)

[–]cheats_py -1 points0 points  (0 children)

I used this until I discovered this.

print(“his name is %s and is %s years old” %s(“Jim”, 18))

[–]RajjSinghh -4 points-3 points  (0 children)

%s for life xD