r/learnpython • • Jul 01 '20

Why do people use .format() method when f string literal exists?

Hello all,

I seem to encounter a lot of documentation and tutorials that use .format() method instead of f string literals.

Personally,

print("I can't seem to {} of a {} why someone {} {} {}".format('think', 'reason', 'would', 'prefer', 'this'))

versus

easily = "easily"
sentence = "sentence"
this = "this"
print(f"A much more {easily} readable {sentence} such as {this}")

F string literal is always much easier to decipher and thus avoid errors.

Perhaps there's some benefit of .format() that I'm unaware of. Googling hasn't brought up much on the debate.

Cheers!

338 Upvotes

100 comments sorted by

50

u/[deleted] Jul 01 '20

f-strings were only introduced in Python 3.6; it's basically nobody's job to go back and update documentation when it still works.

5

u/pw0803 Jul 01 '20

Thanks. I guess don't fix what's not broken..

Though I'm sure I've come across examples post 3.6 where the author has still used .format.

I guess old habits die hard, too.

17

u/Username_RANDINT def get_random_int(): return "RANDINT" # TODO Jul 01 '20

It doesn't mean that once Python 3.6 was released, everyone updated to that version.

str.format even works on Python 2.7.

13

u/[deleted] Jul 01 '20

If you've been using python for 5+ years this is a brand new feature. people use what they're comfortable with.

3

u/[deleted] Jul 01 '20

I jump between 3.6, 3.8 and 2.7 depending on what kind of project I'm working on, so sometimes when I've been working on 2.7 code I get in the habit of using format and slip it into 3.x code.

(I know 2.7 is technically dead but I intermittently work with folks who rely on a software package that remains on 2.7, so *shrug*)

1

u/OnlySeesLastSentence Jul 02 '20

Ironically, a lot of pythin is broken now. Any code that does print"hello" will break.

212

u/[deleted] Jul 01 '20

f-strings exist for convenience. In doing so, they sacrifice portability, flexibility, and functionality. They are not back-wards compatible with python3.5, which is still floating around out there. They also have some drawbacks, like with the normal format method, you can re-use values, e.g.

'{0}, {0}, and {1}'.format('ham', 'spam')

Can't do that with f-strings. you can also build dynamically formatted strings:

a = range(5)
( '{}'*len(a) ).format(*a)

Which can be quite complex. there are probably more reasons others will comment about, but in general, f-strings are just for hard-coded generic messages that aren't anything special. the format function is still very necessary.

117

u/__nickerbocker__ Jul 01 '20

A couple more examples of when to use format instead of fstring are when you have a long string with perhaps long args, and also if you want to throw a dictionary at the string and pull out only the keys that are relevant to the string.

print('I am {} long string and I go on for a while. {} it is more convenient to use {}'.format(
    'very', 'Sometimes', 'str.format'
))

d = dict(name='example', description='This is an example', other=12, data=18)
print('I catch kwargs... {name}: {description}'.format(**d))

28

u/pw0803 Jul 01 '20

Thanks a great little trick. Thanks both.

3

u/Jerscovad Jul 01 '20

Great example!

2

u/RazzleStorm Jul 01 '20

In the second example, if you used an f string to do the same thing, wouldn’t you be able to do {d[“name”]}: {d[“description”]}? Would there be a situation where it’s more advantageous to unpack the entire dict in .format() instead of just the kwargs you need to use (it is cool that you can unpack and use the kwargs that way though)?

4

u/__nickerbocker__ Jul 01 '20 edited Jul 01 '20

Yes you can do that and it would actually be a micro optimization, but you can use the other way when you're coding quick and nanoseconds don't really matter. Most of the time I use it is with template strings... This is a stupid example but I think it makes the point.

import calendar, textwrap

def main():
    cal = {mn:num for num, mn in enumerate(calendar.month_name)}
    template = textwrap.dedent("""\
        Months by number:
            Jan: {January}
            Feb: {February}
    """)
    print(template.format(**cal))

main()

1

u/RazzleStorm Jul 01 '20

Ooh I think I understand the use case a bit better with that example. Thank you!

2

u/veni_vidi_risi Jul 01 '20

Forgive me - what’s the ** in front of the d for? I was also confused by the previous example of the * in front of the a.

7

u/__nickerbocker__ Jul 01 '20

Those unpack the dictionary so the caller receives the key-value pairs as keyword arguments. Example:

>>> def func(a, b, c):
...     print(a, b, c)
...
>>> d = dict(a=1, b=2, c=3)
>>> func(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func() missing 2 required positional arguments: 'b' and 'c'
>>> func(**d)
1 2 3

1

u/jamesonwhiskers Jul 02 '20

Why wouldn't this work in an fstring? Could you not do, print( f 'These are args: {**d}' ) ?

20

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Jul 01 '20

Came here to say exactly this. F-strings are wonderful, but they just aren't fit for every task.

Hell, on long strings with a ton of variables, str.format is easier to keep clean and readable.

15

u/Decency Jul 01 '20

Weird, I completely disagree. With format you have to continue going back and forth between the end of the string and the various places where variables are substituted in order to read the string from front to back. With an f-string it's extremely clear where each variable is being substituted. You can get around this with format by duplicating variable usage, but at that point why bother?

7

u/Sigg3net Jul 01 '20 edited Jul 01 '20

It's not weird.

The parent emphasizes syntax, while you emphasize readability.

Not sure why you're being downvoted. Both aspects and code management strategies have their own merits.

8

u/krakenant Jul 01 '20

Readability of code is one of the fundamentals of python programming...

5

u/Sigg3net Jul 01 '20

Yes, and that is what I would prefer personally. But it's a code management style choice, sort of. Do you want the syntax to be as stable as possible or do you sacrifice syntax reusability for human readability?

For instance, the latter is a big plus if there's a high number of programmers or big turnover. But if it's a small, stable group with established, internal conventions the story might be different.

-5

u/b4xt3r Jul 01 '20

With an f-string it's extremely clear where each variable is being substituted.

print("How {} this not clear {} variables exist in this {}".format(is, where, line)))

1 2 3. 1, 2, 3

11

u/Decency Jul 01 '20

That's not a long string. But even still it's dramatically less readable than:

print(f"How {is} this not clear {where} variables exist in this {line}")

which you can just read left to right.

1

u/b4xt3r Jul 01 '20

Well that didn't line up for crap.

1

u/krakenant Jul 02 '20

Its not a matter of being clear that there are variables, its a matter of being clear what variable goes where for me. You have to count the number of variables you are on. Variables should generally be well named for what they represent. So in an f string, its reading the sentence instead of read, count in the variable list, read, count in the variable list, etc.

10

u/[deleted] Jul 01 '20

python3.5, which is still floating around out there

I just upgraded a program at work from 2.7 to ... 3.4 :(

8

u/[deleted] Jul 01 '20

At least it's not 2 though :D

3

u/[deleted] Jul 01 '20

Yeah. Product didn’t seem to understand the need for upgrading so I ended up doing it in my free time. Not happy about it

2

u/[deleted] Jul 01 '20

yikes :/ Why 3.4? old server?

1

u/[deleted] Jul 01 '20

Yep. The server running that job only does 3.4, so I worked with what I had. I think I aimed for 3.5 because that's what most of our applications are on, but that server is much older. Ultimately there were no python changes but I did have to mess with pip versions and some pinned packages for a while.

5

u/Skasch Jul 01 '20 edited Jul 01 '20

I use f-strings a lot, but I definitely agree with you. One practical situation where I use .format is when I want to declare my format in a different place than where I want to use it.

That notably happens when I define classes, especially with custom __str__ implementations: in that case, I will often declare my string format as a class attribute, then call the .format method on it. Like so:

class Frac:
    FORMAT = "{num} / {den}"

    def __init__(self, num: int, den: int):
        self.num = num
        self.den = den

    def __str__(self) -> str:
        return self.FORMAT.format(**self.__dict__)

2

u/studio-salamander Jul 13 '20 edited Jul 13 '20

In practical terms, this is perhaps the only reason why we can still use .format() - use it where "templating" is necessary, in some way

SOME_TEXT = '{0} some text {1}'
...
use_const = SOME_TEXT.format(var1, var2)

All other cases can be reduced to using f-strings without undue stress

2

u/studio-salamander Jul 13 '20

They also have some drawbacks, like with the normal format method, you can re-use values... Can't do that with f-strings.

>> '{0}, {0}, and {1}'.format('ham', 'spam')
'ham, ham, and spam'
>> ham = 'ham'
>> spam = 'spam'
>> f'{ham}, {ham}, and {spam}'
'ham, ham, and spam'

2

u/irishfarmereng Jul 01 '20

Very good question. Thanks for asking it. I've learnt loads from this thread

1

u/pw0803 Jul 01 '20

Thank you! Me too. Was surprised I couldn't find it asked so succinctly whenever googling this.

2

u/b4xt3r Jul 01 '20

I disagree that it is easier to decipher as you say. I find that keeping all the variables at the end of the printed line makes things easier to scan when you need to look at a lot print lines all at once ('cat myScript.py | grep format' is a nice way to do this. If you have a file open with vi for instance (and why wouldn't you?) and you wanted to find every instance where you were printing lines and you wanted to see the group of variables you just hop line to line using 'format' as the string. It's super-simple.

1

u/pw0803 Jul 01 '20

I suppose in my example it was perhaps written conveniently; certainly if the variables weren't so verbose and perhaps a hundred lines above the f string it wouldn't be quite so easy to decipher.

2

u/Donnshin Jul 01 '20

Currently going through a course where F-Strings are king. This thread gives me a lot of pause in that I shouldn't outright dismiss .format().

2

u/pw0803 Jul 01 '20

My thoughts too!

2

u/SeniorPythonDev Senior Python Freelancer - contact me with jobs Jul 02 '20

Compatibility with previous versions of python.

If you are making the program just for yourself, use f strings, but if you are putting a package on pip for example i would advise to stay away for compatibility reasons with previous versions that people WILL be using with your package.

It also may be company policy or clients refuse to update to a modern version.

1

u/pw0803 Jul 02 '20

That's a great point about sharing with others. Thanks!

2

u/andro38 Jul 01 '20

I am still learner and I use format when I already have variables and I just put them together with format, can someone with more experience tell me should I learn more about and use f string or countinue using format?

4

u/Manny__C Jul 01 '20

You are fine with format. F-strings just look a bit nicer and are easier to write down.

name = 'Manny'
age = 200
a = "I am {} and I am {} years old".format(name, age)
b = f"I am {name} and I am {age} years old"

In the example above a and b are the same string. I guess it doesn't hurt to read how they work from the documentation and to use them every now and then. They are not that complicated after all.

1

u/pw0803 Jul 01 '20

Indeed though I personally find b to be more verbose but am grateful for asking this question because there are plenty of benefits to a.

1

u/Manny__C Jul 02 '20

b is more verbose?

3

u/i_suckatjavascript Jul 01 '20

F string looks better and it’s more organized. Part of the ways Python developers want Python to be more readable and easy to understand.

10

u/efmccurdy Jul 01 '20

Does anyone feel that fstrings mix presentation with code, harming "separation of concerns"?

https://en.wikipedia.org/wiki/Separation_of_concerns

For example, how can fstrings support internationization (without failing wrt DRY)?

11

u/Manny__C Jul 01 '20

I think fstrings were meant to be used for small and quick things and their purpose is just to make the code look tidier, rather than allowing the programmer to code inside strings.

For example, this is a good usage. It's nice and clear and no coding is going on in the string

me = Person()
print(f"My name is {me.name} {me.lastname} and I'm {me.age}")

Whereas this is an example of some code being run, but I think nobody should use them like that

n=20
print(f"The primes less than {n} are {[a for a in range(2,n+1) if all([a%b!=0 for b in range(2,a)])]}")

9

u/zurtex Jul 01 '20

With just a small amount of f-string nesting and walrus operator magic we can really improve this code by:

  1. Making it entirely in 1 f-string

  2. Asking the user for input

  3. Printing each prime on a new line with comma formatting for 4 digit numbers or higher

Here you go (you're welcome):

print(
    f'{str((n:=int(input("Provide a positive integer: "))))[0:0]}'
    f'The primes less than {n} are: {chr(10)}'
    f'{f"{chr(10)}".join(f"{a:,}" for a in range(2,n+1) if all([a%b!=0 for b in range(2,a)]))}'
)

I've made it multi-line for "clarity" but you can simply put the string as 1 line like this:

print(f'{str((n:=int(input("Provide a positive integer: "))))[0:0]}The primes less than {n} are: {chr(10)}{f"{chr(10)}".join(f"{a:,}" for a in range(2,n+1) if all([a%b!=0 for b in range(2,a)]))}')

6

u/Brekkjern Jul 01 '20

Thanks, I hate it.

3

u/canbooo Jul 01 '20

This. Separation of concerns is important but too much separation makes code complex. The art is find just the right amount. Here is a link with more links.

(Can you spot the irony in my answer?)

2

u/[deleted] Jul 01 '20

Coding in the f string brackets is a no for me. I always do that outside of it. Only a variable goes in f string brackets!

2

u/duquesne419 Jul 02 '20

I've done a function call when I needed to insert math into a string(ie calculate change or something simple, I'm a noob), but yeah, putting a list comprehension in format brackets is wild, I don't think I like the readability of that.

1

u/toastedstapler import itertools Jul 02 '20

the scope of an f-string is very small so the issue isn't too major. it's not like a react component where you can mix in loads of logic with its display

1

u/jwnskanzkwk Jul 01 '20

I'm still using % for string formatting xD

"hello %s" % (world)

1

u/pw0803 Jul 01 '20

Old ways are the best, ha!

Is this because of habit or is there a good reason?

I noticed psycopg2 parsing parameters to SQL query documentation is the same and wondered if there's an inherent advantage in doing so.

Perhaps from their perspective it's better to maintain legacy compatibility since there's a good chance this is exactly the kind of code that could get written and be left forgotten about in some legacy system for years.

1

u/jwnskanzkwk Jul 01 '20

Using something else is usually better in most cases. But i've been using Python for a long time and am too lazy to check the documentation :/

1

u/pw0803 Jul 01 '20

Fair enough, well, I'd say it's just as easy if not more!

3

u/smajl87 Jul 01 '20

Last time I checked f-strings didn't work well with translations, i18n

3

u/Teoweoha Jul 01 '20

Because as a Linux System Administrator I often write Python scripts for automated monitoring, but the systems are old and Python 3.4 is the highest version available. We can try to insist that our customers upgrade, but I'm often asked to make the old dog do new tricks while we try to convince them to purchase something newer.

5

u/HobbeScotch Jul 01 '20

It could be argued that .format() is more explicit and generally speaking explicit > implicit. Also because python isn’t statically typed it can be safer to explicitly .format() a string and know what’s getting passed.

3

u/krakenant Jul 01 '20

What would the argument be for it being more explicit? F sting literally says, insert this variable here. I don't think it's possible to get more explicit than that.

.format basically uses positional variables which from experience are nice for one or two variables but a complete mess for much more than that.

1

u/HobbeScotch Jul 02 '20 edited Jul 02 '20

.format() takes both positional and named arguments. So you can use .format(keyword='string') to pass variables to 'this is a {keyword}'. And, because it takes named arguments you can directly pass a dict to format a string with .format(**my_dict). It can elegantly take many of variables.

I suppose my issue is that F string just says, "find a variable with this name from anywhere in the namespace and put it in my string". Meanwhile, that variable could have been altered or overwritten because python doesn't perform type checks.

Although there is not really much of a practical difference, when you actively/explicitly pass a variable to a string with .format(), I believe you're less likely to make mistakes stemming from those limitations of python.

1

u/smurpau Jul 02 '20

print(f"How could value: {value} possibly be any more explicit?")

1

u/humanitysucks999 Jul 01 '20

I'm not sure if f-string has this, but with format you can have stylized printing, left align, right aligh, fill stars, etc.

1

u/pw0803 Jul 01 '20

I wasn't aware of that! How is that achieved?

1

u/humanitysucks999 Jul 02 '20 edited Jul 02 '20

https://docs.python.org/3/library/string.html#formatspec

It's useful if you're printing formatted text reports instead of raw data dumps. I come from COBOL background so I'm used to doing this the ugly way lol. Alternatively people would build web interfaces to do shit with data, I never got to learning that part, so text reports and csv dumps for me

1

u/parnmatt parnmatt Jul 01 '20

it does, in format the index is optional (except in Python 2.6 when it is required), but it appears before the colon '{:5.2f}'.format(num) is '{0:5.2f}'.format(num) whereas an f-string doesn't use indices, it uses expressions, so f'{num:5.2f}'

personally, if I am doing any form of formatting, I would use format, it's simply less busy by my eye. fstrings are really only clearer for trivial strings.

2

u/humanitysucks999 Jul 01 '20

Thanks for the info. I don't think I've ever used fstring, so I wasn't really sure.

1

u/i_suckatjavascript Jul 01 '20 edited Jul 01 '20

Because they are old skool and people aren’t up to date with the latest from Python. Even Udemy doesn’t accept f strings on their coding exercises. I personally like f strings better.

1

u/pw0803 Jul 01 '20

I learnt about both from Jose Portilla's course so I don't think it's forbidden; though he definitely prefers .format method.

1

u/i_suckatjavascript Jul 02 '20

I didn’t took his course. I took Colt Steele’s course. And he prefers the f string because it’s neat and quicker.

2

u/MyPythonDontWantNone Jul 01 '20

The .format style is closer to how the loggung library works. This can technically be done with f-strings, but the .format style allows unlogged events to not be processed which may have a performance increase.

2

u/_pandamonium Jul 01 '20

Maybe I just don't know how to use f strings properly, but I use the format method to quickly print out tables. I'm on my phone so I probably won't explain it very well, but here's a simple example to print out a "table" with 2 columns, a number and the square of that number:

numbers = range(10)
template = "{} {}"
for n in numbers:
    print(template.format(n, n**2))

This example looks really stupid because I only use the template once inside the loop, but there are situations where it makes more sense, I promise. The point is I could re-use the template in other places, maybe the code does something that changes "numbers", etc.

I'm sure someone will come along and tell me why this is wrong and how I could avoid doing it. But it's a habit. Also I use vim and it's easier to go to the end of a long string and edit the arguments of the format method, instead of going to the middle of the string to do it. So I guess it's mostly just a lazy habit.

2

u/F-Strings Jul 02 '20

Ahh format...my arch nemesis!

2

u/prettysureitsmaddie Jul 01 '20

In most cases I have seen it's legacy from earlier versions of python.

1

u/[deleted] Jul 01 '20

Mostly people are either using an older version of python (f-strings where only introduced in 3.6) or they are doing it out of habit.

There are probably still reasons to use .format() but in most cases if your version is up-to-date you'd wanna use f-strings

1

u/jcampbelly Jul 01 '20

Also formatters like '{!r}' when you want a __repr__ version or float/int padding and significant figures with '{:.2f}'. It's also nice to be able to build a kwargs object and pass it as .format(**kwargs). You can use '{key} ...' to retain readability.

1

u/[deleted] Jul 01 '20

f'strings have only been a formal thing since 3.6 (?). A lot of documentation simply does not use it for that reason.

1

u/[deleted] Jul 01 '20

I use it when my lines get too long.

Sometimes it seems nicer to write the variables out on new lines, comma separated.

“whatever {}”.format(
     self. this_is_getting_super_long,
      ...
 )

Or

f”whatever {self.this_is_getting_super_long}”

Obviously helps a lot when classes and your indentation gets out there.

1

u/Se7enLC Jul 02 '20

Because it only exists in python 3.6 and newer. Sometimes people only have 3.5.

I don't have a good reason for why those people don't upgrade python. But I also don't think convenient string formatting is a compelling enough feature to limit compatibility.

When I write code, I try to keep both external library requirements and version requirements low. When I bump them up it's for a really good reason (a feature I really need). There will eventually be something that is only available in a newer python version that is worth forcing that on my users. But this isn't it.

I've had to go through a file and revert f-string changes before. It sucks.

1

u/__te__ palimpset Jul 02 '20

Organization: Generally, you can only define an f-string when you have all of the variables defined for it. If there is a loop, and the f-string uses a variable generated by the loop, the f-string must be inside the loop. If there is a deep, nested function which generates an internal variable and you want to use it in the f-string, you must either exfiltrate that variable, or bury the f-string inside that function. Sometimes this is the best way to organize the code. Sometimes it's the worst. Older-style formatting allows you to choose.

In addition, two other answers being repeated a lot in this thread:

Legacy: I hate it, but we still have to work with older code sometimes. F-strings are only available from python3.6 onward.

i18n: The current standard way of doing Internationalization is to maintain a key:value list of messages for each locality, with the key being the string without variable interpolation (i.e., "Hello, {}" rather than "Hello, Aubrey", because you can't do "Hello, Aubrey" for every name possible). When you are ready to translate, you can then do a lookup. f-strings are never actually accessible in their non-interpolated form, however. It breaks this methodology. Instead, you need to have a separate f-string for each locality.

1

u/OnlySeesLastSentence Jul 02 '20

I didn't learn about them until covid. My reason: I'm a C guy. We use printf. Also, back when I did python, you guys didn't have f strings. So when I took up Putin again two months ago, people were like "use fstring" and I was like "nah, don't feel like learning something new".

Then I had trouble with .format and realized "oh, it's similar to printf, neat".

1

u/[deleted] Jul 02 '20

Not sure if this was mentioned before, but I definitely wrote this multiple times:

  1. Some people, on principle, will not use f-strings because they break their tools. f-strings bring so little value that not using them makes your programs simply easier to understand and that's about it.

  2. format() is modular. You can extend the format language, you can also globally affect things in retrospect. It has all the desirable (and less so) features of OO design. Not a lot of people actually use format() to its full potential, but those who do, would never trade it for f-strings.

I.e. in exceptionally trivial cases, f-strings are OK. If you want your code to be robust, easy to maintain, easy to modify in retrospect, easy to integrate with other tools, then you will quickly discover that f-strings are not the right tool.

Consider this example: you want to be able to configure logging verbosity. This means, that your logging messages may not need to be displayed some of the time. If the message doesn't need to be displayed, then doing the interpolation work is unnecessary (and, some times may be just burning a lot of CPU cycles for nothing). So, you want your logging API to accept the mask and the values to interpolate into it, but to make decisions about interpolating them later, once you know that the message is going to get printed. There's no way to accomplish this with f-strings.

1

u/Brian Jul 02 '20

There's one very important aspect of .format that makes it entirely necessary and cannot be replaced by f-strings: it is dynamic, while f-strings can only operate on static embedded strings in the source code (and this must be the case for important security reasons).

That is, .format() can be passed a string from anywhere - not just a string literal created at that point in the source code. There are a lot of important cases where this matters, but one of the most important ones is internationalisation and localisation. Ie. the strings you display to the user need to be translated to their language. You don't want to have to rewrite the sourcecode for every language, and instead, will want to obtain the current language's version of the string from external sources (eg. gettext catalogs). This means that in practice, for any widely used software that needs to be translated, most usages of f-strings (ie. anything constructing text to show the user) should really be replaced by .format, with a lookup of the string to display.

Another such usecase is logging, where you generally don't want to do the replacement unless you've determined it's not just going to be ignored, to avoid performance impact.

1

u/phunkwad Jul 08 '20

Thank you for mentioning the static versus dynamic distinction. That finally nailed it for me.

1

u/Paddy3118 Jul 22 '20

Wouldn't this work?

1   easily = internationalise(localise("easily"))
2   sentence = internationalise(localise("sentence"))
3   this = internationalise(localise("this"))
4   print(f"A much more {easily} readable {sentence} such as {this}")

1

u/phunkwad Jul 24 '20

It does work for those three words, but not the rest of the string.

0

u/[deleted] Jul 01 '20

[deleted]

5

u/pw0803 Jul 01 '20

Oops! Well spotted.

0

u/[deleted] Jul 01 '20

[deleted]

1

u/pw0803 Jul 01 '20

Great reply, thank you!

And yes, admittedly it was a contrived and very convenient example.. alas I wanted to try and demonstrate its simplicity for how I view it, albeit lacking in real-world probability..

0

u/[deleted] Jul 01 '20

[deleted]

1

u/I_had_to_know_too Jul 02 '20

I still have to support a ton of RHEL5 and Python 2.4 nonsense. Almost all of our Python has to run on 2.4...

I wish I could use f-strings and the new walrus operator. I wish I could use so many things.

0

u/[deleted] Jul 01 '20

[deleted]

4

u/[deleted] Jul 01 '20

There was nothing cocky about the OPs question. Now talk about a grumpy reply! Jeez!

2

u/i_suckatjavascript Jul 01 '20 edited Jul 01 '20

Grumpy replies like these belong to StackOverflow.

On the topic of grumpy replies, I remember making a grumpy program on Python where if you ask it for information, it gives you a grumpy reply, following the answer, from tutorial on Udemy LOL

0

u/[deleted] Jul 01 '20

lol

0

u/[deleted] Jul 01 '20

[deleted]

1

u/Corm Jul 01 '20

What do you mean they don't work nicely?

18

u/[deleted] Jul 01 '20

[removed] — view removed comment

7

u/Stepthinkrepeat Jul 01 '20 edited Jul 01 '20

print(f"I can't seem to {words[0]} of a {words[1]} why someone {words[2]} {words[3]} {words[4]}")

Does this not work?

Edit: words[2:] doesn't unpack as a string, would stay in a list format on output.

Could use different methods to unpack - for loops, list comprehension, mapping with join

{' , '.join(map(str, words[2:]))}

0

u/[deleted] Jul 01 '20

[deleted]

1

u/Stepthinkrepeat Jul 01 '20

Updated and left a note about that