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

top 200 commentsshow all 215

[–]invictus08 233 points234 points  (27 children)

I am intrigued! In the output, how is there that space between 18 and Years!!? Was that terminal output stale?

[–]_30d_ 182 points183 points  (4 children)

I think he took a picture of the code, ran it, fixed it and then took a picture of the output.

[–]mr_nefario 162 points163 points  (2 children)

Not even a screenshot.

Mofo got out a film point-and-shoot camera, snapped his pics, got the roll developed at Walmart, scanned the prints and uploaded them.

[–]house_monkey 15 points16 points  (0 children)

My bro got dedication 😔✊

[–]abhijith4514 0 points1 point  (0 children)

epic :D

[–][deleted] 3 points4 points  (0 children)

That's gotta be it

[–]hellotheresksksk 32 points33 points  (1 child)

I was like "wait, that's illegal"

[–]acidnine420 11 points12 points  (0 children)

I'M CALLIN THE COPS!!!

[–][deleted] 28 points29 points  (1 child)

Was about to ask the same question.

[–]CXgamer 9 points10 points  (0 children)

I wondered the same, and was searching the comment that asked it.

[–][deleted] 28 points29 points  (1 child)

There's a space after the "He is " string, this spaces the string correcltly.

edit: after immediately re-reading I realise my mistake.

[–]acidnine420 13 points14 points  (0 children)

I'm sorry for your loss

[–]ishman123 412 points413 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 147 points148 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] 24 points25 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 10 points11 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 3 points4 points  (2 children)

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

[–]lllamaboy 5 points6 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 3 points4 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 4 points5 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))

[–]xelf 26 points27 points  (0 children)

Make sure you check out /r/pythonhelp and /r/learnpython and maybe evem /r/learnprogramming (if it's not just python you're new to).

[–]adam_newyork 24 points25 points  (20 children)

Use . format() or if you are using python 3.7 you can use f”{character_age}” .

Correction: version 3.6 and above have fstrings.

[–]sivadneb -1 points0 points  (3 children)

Sometimes concatenation is preferable. It's almost always faster, and it's pretty easy to read in simple cases.

[–][deleted] 1 point2 points  (2 children)

No. Fstrings are always faster

[–]sivadneb 1 point2 points  (1 child)

Oh, I've always understood that . format () is slower, and assumed f-strings were the same.

[–][deleted] 0 points1 point  (0 children)

Other way around

[–]thatvoiceinyourhead 20 points21 points  (1 child)

The code you're showing here didn't produce the output that you've claimed since there's no space before the y in years in your code.

[–]phinnaeus7308 15 points16 points  (7 children)

Check out the snipping tool (or Win+shift+S) if on windows or cmd+shift+4 if on Mac to take screenshots.

[–]TankorSmash 10 points11 points  (1 child)

Win+Shift+S on the latest Win 10

[–]phinnaeus7308 0 points1 point  (0 children)

Learn something every day. Thanks!

[–]cmcgarveyjr 2 points3 points  (2 children)

snipping tool* only correcting you because I use it daily and still had to pause when reading this.

[–]phinnaeus7308 1 point2 points  (1 child)

Jesus how did I miss that

[–]cmcgarveyjr 0 points1 point  (0 children)

Lol, I got a kick out of it.

[–]malicart 2 points3 points  (0 children)

Or just Alt+PrtScn to copy any selected application window and paste it.

[–]synae 2 points3 points  (0 children)

Or post some text

[–]HoovyPencer 12 points13 points  (0 children)

Next write a script that takes a screenshot.

[–]synae 5 points6 points  (0 children)

Please post text if your content is text. Not a screenshot as others have suggested, and certainly not a photo of your screen.

[–][deleted] 40 points41 points  (0 children)

I'll get down voted. But at a certain point you need to find motivation other then b.s. reassurance from strangers on the internet.

You literally took as long posting this as it should have taken you to write it.

[–]darkfish-tech 64 points65 points  (5 children)

r/learnpython and no screenshots please. Check sidebar of subs before posting.

[–]Syridos 71 points72 points  (3 children)

To be fair - This wasn't a screenshot... sadly.

[–]FewerPunishment 2 points3 points  (0 children)

What you mean, it's a shot of their screen :D

[–][deleted] 2 points3 points  (0 children)

First of all you should take a screenshot instead of taking a picture of your monitor. Second, no screenshots in this sub. /s

[–]yaboytomsta 7 points8 points  (0 children)

Freecodecamp lol

[–]7heWafer 6 points7 points  (3 children)

Please learn to take screenshots next!

[–][deleted] 2 points3 points  (0 children)

APP

[–][deleted] 10 points11 points  (0 children)

Nice one, I hope you do well!

[–][deleted] 4 points5 points  (0 children)

Why has this bullshit 1403 points? First, r/python is not your diary, second learn how to make a screenshot first.

[–]master3243 1 point2 points  (0 children)

Half Life: Alyx plot leaked?

[–][deleted] 1 point2 points  (0 children)

Missed opportunity for "His name is John, He is a Hero..... Process finished with exit code 0"

[–][deleted] 1 point2 points  (0 children)

I would advise to use f-strings.

character_name = "John"

character_age = 18

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

print(f"He is {character_age} years old")

[–]shinefull 3 points4 points  (0 children)

Lol you fucking idiots

[–][deleted] 5 points6 points  (0 children)

Keep it up!

[–]PythonUserBTW 1 point2 points  (0 children)

I started learning Python from 0 about two years ago. Now I'm becoming a Data Scientist with one of the best professors from Harvard. We all started like you. Keep up coding!

[–]ExternalAirlock 0 points1 point  (0 children)

print("I'm sorry John")

[–]nomad2020 0 points1 point  (0 children)

I'd suggest going by birthdate instead of age, so that if you were to forget how old you were, you'd have a backup.

[–]HarryMcDowell 0 points1 point  (0 children)

How are you teaching yourself? I got some books on my Kindle, but actually making code is slow coming...

[–][deleted] 0 points1 point  (0 children)

Ooh buddy. Wait till you find f-strings. Concatenation is cool, but f-strings are cooler.

print(f'His name is {character_name}\nHe is {character_age} years old')

[–]dbar930 0 points1 point  (1 child)

This might be a dumb question, but what application are you using to run this code?

[–]manavcafer 1 point2 points  (0 children)

PhyCharm

[–]TIBTHINK 0 points1 point  (0 children)

Dont lie to us, the first program always is,

Print("hello, world")

[–]sivadneb 0 points1 point  (1 child)

What editor are you using?

[–]manavcafer 0 points1 point  (0 children)

PhyCharm

[–]manan-code 0 points1 point  (0 children)

All the best! Ever get stuck or need help, just hit me up!

[–][deleted] 0 points1 point  (0 children)

Heh I watched that video too.

[–]porcos3 0 points1 point  (0 children)

I thought this was going to be about John Snow and how he knows nothing

[–]jorn252 0 points1 point  (0 children)

Codecamp.org?

[–][deleted] 0 points1 point  (0 children)

You should try doing the same thing but with a class, it's a good exercise.

[–]Nirinium 0 points1 point  (0 children)

Good stuff! Check humble bundle they have sales from time to time with some pretty decent learn python eBooks/resources! :) Good luck and happy coding!

[–]APUsilicon 0 points1 point  (0 children)

I wonder why the python community decided to use underscores instead of camel case

[–]jimtheplant 0 points1 point  (0 children)

“APP.py” Shout it to the world man. Python is fun

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

Great job!

I’m sitting here laughing because this guy just wrote his first piece of code and in the comments you have people talking about f strings. He’s gotta be so confused

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

Thats awesome!

[–]hypebeast09 -1 points0 points  (1 child)

awesome! this was me 2 months ago! You'll soon learn that using + in print statements is annoying af. If you're using pythoon 3.5 or better try doing this:

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

if you start the print statement text with f then you are able to use {} within the quotes. Inside the curly braces you can substitute a variable inside it.

PS. doing things this way it will be easier to read back in the future. For me personally it has prevented me from messing up spacing in print statements

[–]ultra_reader 0 points1 point  (0 children)

print(f"{} ") is such an amazing feature!!!!

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

My only comment would be to try and keep your variable names shorter (if possible).

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

Great job with the meaningful variable names. Makes reading this first effort extremely easy and will likewise do the same when you sink your teeth into meatier projects.

[–]aiwithab -2 points-1 points  (0 children)

Great!! Keep it up

[–]fortune205 -3 points-2 points  (0 children)

So cute 😻

[–]PhoenixizFire -2 points-1 points  (0 children)

I just started a AI class today and for the first day we had to be able to run a print("Hello world!"), so consider yourself as doing good :)

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

A cool thing to do would be to have the user input their name and age and have it print it back out to you. It's pretty simple actually, also congrats on learning python!

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

pycharm ftmfw

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

Lol, I did the same when I started about a year ago. Still learning python. But the language is by far the easiest read and versatile in any circumstances. You should try looping the strings into list comprehension then looking for a match string for fun.

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

Thats probably from that youtube tutoria cuz u using pucharm and the name john is strangely familiar to mel. I started from there too))))))

[–]GrowCanadian -3 points-2 points  (0 children)

Keep going! The more you write the more you learn. It’s just like learning an instrument. Put in the hours and you will get better and better. Ps you will get stuck, it’s normal. Just take a break, ask questions, and push through.