list and for - 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: list and for (/thread-11024.html) |
list and for - Breex - Jun-18-2018 I'm new at python programing i read some basic and i have a question. Probably it is easy but i dont know how to do it. (im looking for answer for 2 days) I have something like this string="awesome cat"; str=string.split(); for i in str: print ('-'.join(i));And if i print it i have a-w-e-s-o-m-e c-a-t And my question is how i make each i into new string like this "a-w-e-s-o-m-e c-a-t"? I know its easy but im very basic at programing so im sorry if i hurt anyone with my question. RE: list and for - anickone - Jun-18-2018 Hello! s="awesome cat" l=s.split() out=[] for s in l: out.append('-'.join(list(s))) print(' '.join(out)) RE: list and for - volcano63 - Jun-18-2018 Just replace space with empty string print('-'.join("awesome cat".replace(' ', '')))and the output is
str is Python built-in type/function, and giving names of built-ins to variables is potentially dangerous. Little demonstration
RE: list and for - Breex - Jun-18-2018 Thanks very much. |