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())
13 Answers
+ 11
s = 'Hi, Friend'
func = lambda x: ''.join([('*','@')[i.isupper()]for i in x])
print(func(s))
+ 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']))
+ 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.
+ 4
Just in case you didn't require a lambda function:
string = 'Hi, Friend'
print(''.join(['@' if i.isupper() else '*' for i in string]))
+ 3
Andrex
txt = 'Hello World'
for i in txt:
if i.isupper():
txt = txt.replace(i, '@')
else:
txt = txt.replace(i, '*')
print(txt)
Output = @*****@****
+ 3
+ 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.
+ 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?
+ 1
Thanks to all!
So many decisions of the same problem)
+ 1
S= input()
L=[]
For i in range (0, len(S)) :
if (i. isupper()) :
L. append(@)
Else:
L. append(*)
Print(s)
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.
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.
0
Assalamu alaikum