Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a word into letters in Python

Tags:

python

I was wondering if there is a straightforward way to do the following:

Input string:

input = 'Hello'

Output string:

output = 'H,e,l,l,o'

I understand you can do list(input), but that returns a list and I wanted to get the string rather than the list.

Any suggestions?

like image 263
Rumman Avatar asked Feb 06 '13 19:02

Rumman


2 Answers

In [1]: ','.join('Hello')
Out[1]: 'H,e,l,l,o'

This makes use of the fact that strings are iterable and yield the individual characters when iterated over.

like image 103
NPE Avatar answered Sep 27 '22 20:09

NPE


outputstr = ','.join(inputstr)
like image 31
PaulMcG Avatar answered Sep 27 '22 22:09

PaulMcG