0

How can i replace uppercase symbols to @, but other to *. Expected result looks like @***@***** from a string 'Hi, Friend'

#My not quite correct code: func = lambda x = 'hi, Fri': ''.join(x[c] if x[c] in '' for c in (0, len(x)-1) else '*' for c in range(len(x))) ''.join(x[c] if x[c].islower in '' or c in (0, len(x)-1) else '@' for c in range(len(x))) print(func())

20th Mar 2020, 12:29 PM
Andrey Shintar
Andrey Shintar - avatar
13 Respuestas
+ 11
s = 'Hi, Friend' func = lambda x: ''.join([('*','@')[i.isupper()]for i in x]) print(func(s))
20th Mar 2020, 12:38 PM
Louis
Louis - avatar
+ 7
Ditus The Zen of Python is somewhat subjective. But in this case I think you should not confuse one liners with list comprehension All oneliners rely heavily on list comprehension, but not all list comprehension are oneliners. If I were trying to make a oneliner I would have written print( ''.join([('*','@')[i.isupper()]for i in 'Hi, Friend']))
20th Mar 2020, 7:24 PM
Louis
Louis - avatar
+ 5
The Zen of Python is poetry. Yet, it says that code should be beautiful, simple, and readable. In my opinion this is the complete opposite of the classic one-liners which try to minimize character count and avoid line feed. I prefer writing 4-5 lines that are obvious and self explaining, instead of a convoluted oneliner.
20th Mar 2020, 7:34 PM
Tibor Santa
Tibor Santa - avatar
+ 4
Just in case you didn't require a lambda function: string = 'Hi, Friend' print(''.join(['@' if i.isupper() else '*' for i in string]))
20th Mar 2020, 12:44 PM
Russ
Russ - avatar
+ 3
Andrex txt = 'Hello World' for i in txt: if i.isupper(): txt = txt.replace(i, '@') else: txt = txt.replace(i, '*') print(txt) Output = @*****@****
20th Mar 2020, 12:42 PM
maf
maf - avatar
20th Mar 2020, 12:44 PM
Gordon
Gordon - avatar
+ 3
Comprehensions are great and I love them too, I just don't like abusing them. But this is a matter of programming style. You could look at a one-liner for half an hour and still remain puzzled. When you look at the cleanly neatly written variant that does the same thing, you get the logic in a matter of seconds. That is a huge difference in readability. Most typically, software is written once, but read, interpreted, debugged and refactored by various people many times. That is why style matters, because it should be crystal clear to any programmer who reads your code for the first time, what is the intention and how it works. I consider the ability of writing such code, a better and more useful skill than writing compact oneliners.
20th Mar 2020, 8:00 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Louis I am studying python since a few weeks intensively and I know that Zen of python suggests that solutions should look more like Andrex' suggestion, but personally I think the beauty in python are exactly those beautiful oneliners like yours that one could not think of in other languages. Or do I misunderstand the Zen of python?
20th Mar 2020, 1:28 PM
Ditus
Ditus - avatar
+ 1
Thanks to all! So many decisions of the same problem)
20th Mar 2020, 1:33 PM
Andrey Shintar
Andrey Shintar - avatar
+ 1
S= input() L=[] For i in range (0, len(S)) : if (i. isupper()) : L. append(@) Else: L. append(*) Print(s)
22nd Mar 2020, 11:16 AM
Syed
Syed - avatar
0
It is just confusing that python gives us this beautiful concept of list comprehension and joins and maps to form the most beautiful one liners I know and then the Zen suggests that there should be one obvious and simple way, which sounds to me like minimalistic statements and many rows.
20th Mar 2020, 7:47 PM
Ditus
Ditus - avatar
0
We got a little offtopic here from the original question, but when it comes to programming style, I can't resist posting this video https://youtu.be/OSGv2VnC0go Raymond Hettinger is one of the key contributors to modern python. I learned a lot from this talk, but also his other videos are worth watching.
20th Mar 2020, 8:15 PM
Tibor Santa
Tibor Santa - avatar
0
Assalamu alaikum
21st Mar 2021, 10:59 AM
Abbu Syed
Abbu Syed - avatar