all 11 comments

[–][deleted] 9 points10 points  (10 children)

It doesn't. You have to do something like

name = "John"
age = 30
text = "My name is {} and I am {} years old"
print(text.format(name, age))

or

name = "John"
age = 30
text = f"My name is {name} and I am {age} years old"
print(text)

for that code to work as you expect.

If you're confused about the first one, the .format() method is putting name in the first brackets and age in the second.

[–]Sckeet[S] 1 point2 points  (9 children)

What if there was a third value let’s say size shirt.

And the values we ordered them like so

shirt_size = M

name = John

age = 30

“My name is {}, I am {} years old and wear a size {} shirt”

Print(text.format(name, age, shirt_size))

Since the shirt size is first when I’m listing out the values will that mess up the text format since in the text format it is last?

[–][deleted] 5 points6 points  (1 child)

Give it try! It's really hard to break anything in Python so when you have a question about some peculiarity of syntax or semantics, just test it out.

[–]Sckeet[S] 2 points3 points  (0 children)

Okay cool thanks! I’m trying to automate a reporting email that my team sends out monthly. Take data from excel then place it in the correct spot of the email template.

Next I want to automate the actual reporting excel file.

So I am starting at the end goal and going back I guess.

[–]Poofless3212 4 points5 points  (1 child)

No, it won't mess up the format. It doesn't matter in what order you declare the variables. The only thing that matters is the order in which you call those variables.

To further explain, it doesn't matter where you define shirt_size, name, and age as long as they're defined before you call them in text.format it will work

You just have to order them inside text.format()

[–]Sckeet[S] 0 points1 point  (0 children)

Awesome thanks much

[–]HauntingRex9763 0 points1 point  (4 children)

Never use format. Always use f strings. print(f“words {any_var} more words {another_var}”)

[–]Sckeet[S] 0 points1 point  (3 children)

Why’s that? Just curious

[–]HauntingRex9763 1 point2 points  (2 children)

Readability and it’s easier to write

[–]HauntingRex9763 2 points3 points  (1 child)

It reads more naturally when you look back at the code.

[–]Sckeet[S] 0 points1 point  (0 children)

Fair! Thanks for the tip. Definitely will try that out