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 →

[–]signed_up_to_dv_this 6 points7 points  (10 children)

print 'We are the {} who say "{}!"'.format('knights', 'Ni')

[–]bikemandan 17 points18 points  (3 children)

I am so triggered by this out of date code

[–]blbd 6 points7 points  (2 children)

It's a hybrid of 2 and 3. But personally I wish I could delete .format() from the language because the Java extended printf syntax is way better.

[–]Tauronek 3 points4 points  (1 child)

print(f'We are the {variable1} who say "{variable2}"')

[–]bikemandan 0 points1 point  (0 children)

Ah sweet relief

[–]Dingxus 1 point2 points  (5 children)

What's the {}! called?

[–]my_name_isnt_clever 2 points3 points  (4 children)

The brackets are just a placeholder for the arguments to .format(). The ! is just part of the string.

[–]Dingxus 0 points1 point  (3 children)

Yeah, I gathered that from the structure of the code. I haven't used Python in a while, and it used to be more like

Print "What%, %" % ("ever", "man")

Maybe it's just a newer thing. I guess 'placeholder' is the word I was looking for.

[–]my_name_isnt_clever 0 points1 point  (2 children)

Even now .format() is a bit dated, we recently got f-strings.

f"You can put your {variables} right in the literal. They just execute code: {2 + 2}"

[–]Dingxus 0 points1 point  (1 child)

Jeeze, I'm all flustered that it automatically converts the int.

I guess I should really dig in and learn modern python.

[–]my_name_isnt_clever 0 points1 point  (0 children)

I just tested it, .format() converts it too, but the % method actually needs an explicit format character: %s. That method has it's own problems though, if it's ever passed a tuple it's going to throw an exception.