Python Forum
Back again on f-strings - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Back again on f-strings (/thread-21805.html)

Pages: 1 2


RE: Back again on f-strings - newbieAuggie2019 - Oct-15-2019

(Oct-15-2019, 11:52 AM)perfringo Wrote: I agree with buran that his solution is more life-like than oneliner and therefore longer.

One should be defensive and/or generalise (duck-typing) when writing functions. In this particular case - you can through any iterable to it and it behaves as expected. Little longer code now, lot of time saved on debugging in the future.

I'm just a bit shocked that what I thought it was going to be something simple, it is indeed much more complicated than I had expected, probably due to my naïvety and still ongoing huge lack of knowledge.

All the best,

(Oct-15-2019, 12:07 PM)buran Wrote: inflect will not handle list with numerical values, range object or generator expressions. you need to preprocess these and convert to list/tuple with str elements. That's why you get the first error

I guess that I'm going to need much more time than expected sorting out what I had previously thought as an easy-peasy problem. Confused Huh Think

All the best,


RE: Back again on f-strings - newbieAuggie2019 - Oct-16-2019

(Oct-15-2019, 09:40 AM)newbieAuggie2019 Wrote: Hi!

It seems that I cannot fully understand f-strings. I do exercises and little programs trying to update them by using f-strings, but now and then, I keep having some problems.

For instance, I wrote this little program:

# atbs_04_commaCode_01.py
#
# It works.
#

def commaCode(list1):
    print("'", end='')
    print(*list1[:-1], sep=', ', end=",")
    print(' and', list1[-1], end=".'\n\n")
          
spam1 = ['apples', 'bananas', 'tofu', 'cats']
commaCode(spam1)
eggs1 = ['bird', 'dolphin', 'whale', 'parrot', 'gorilla', 'dog']
commaCode(eggs1)
numbers1 = [1, 2, 3, 4, 5]
commaCode(numbers1)
that works and produces the following output:
Output:
'apples, bananas, tofu, and cats.' 'bird, dolphin, whale, parrot, gorilla, and dog.' '1, 2, 3, 4, and 5.' >>>
1) Is there a way to simplify the 3 lines of print with f-strings?
2) Is there a good tutorial FOR DUMMIES or FOR NEWBIES on all the details of f-strings?

(Oct-15-2019, 12:27 PM)newbieAuggie2019 Wrote: I'm just a bit shocked that what I thought it was going to be something simple, it is indeed much more complicated than I had expected, probably due to my naïvety and still ongoing huge lack of knowledge.



I guess that I'm going to need much more time than expected sorting out what I had previously thought as an easy-peasy problem. Confused Huh Think

After giving it some further thought, and seeing that trying to do it with f-strings is more complicated than I originally expected, I think I'm going to follow the saying 'If it ain't broke, don't fix it'. After all, my original program was working and had no problems in dealing with different types of lists, not having to use modules or expressions that I don't fully understand as a newbie.

I thank all of you that have given your thoughts and ideas to my little dilemma, and I'm going to set it solved.

All the best,