Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Back again on f-strings
#11
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#12
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 764 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  i'm back to f-strings, again Skaperen 4 1,086 Sep-21-2022, 08:13 PM
Last Post: Gribouillis
  Splitting strings in list of strings jesse68 3 1,771 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,521 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,232 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020