Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two .join methods into one

Now I have this :

str = "  s  tr & &^ 23G7/*%45afju657**(%^#%$!!fdf"

str = ''.join(e for e in str if e.isalnum())
str = ''.join(('...', str, '...'))

Can I combine them like :

str = ''.join(('...', e for e in str if e.isalnum(), '...'))
like image 395
ratojakuf Avatar asked Aug 29 '26 02:08

ratojakuf


1 Answers

You can use format there

s = "...{}...".format(''.join(e for e in s if e.isalnum()))

As a side note, do not name your string as str as it shadows the builtin

Apart from that, if you really really want to use join twice, you can write it as

''.join(('...', ''.join(e for e in s if e.isalnum()), '...'))

But it is not a good idea. Why use a nuclear bomb to kill a mosquito!

like image 91
Bhargav Rao Avatar answered Aug 30 '26 17:08

Bhargav Rao