0

I'd like to know how separate characters in string

i would expect something like that:

word = 'cool'
~~~~~~~~~~~~ - the code which i dunno
output :
c o o l 
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
Quasi31
  • 37
  • 5

3 Answers3

4

Since strings are iterables of characters, you can use the join method available on strings on strings too.

word = 'cool'
separated_word = ' '.join(word)
print(word)
print(separated_word)

outputs

cool
c o o l

And naturally you can also change the separator:

>>> print(' * '.join(word))
c * o * o * l
AKX
  • 152,115
  • 15
  • 115
  • 172
0

Depending on which way you want to do it, if you want to split it into an array, have a look at this: What's the best way to split a string into fixed length chunks and work with them in Python? and think of it as 'chunks of size 1'

if you just want to loop over and work on each char:

>>> my_str = "cool"
>>> for x in my_str:
...    print("->%s<-" % x)
... 
->c<-
->o<-
->o<-
->l<-
Jmons
  • 1,766
  • 17
  • 28
0

Here is the solution:

test = "cool"

output = ""
for l in test:
    output+=(l+" ")

print(output)
iczyrski
  • 51
  • 10