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
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
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
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<-
Here is the solution:
test = "cool"
output = ""
for l in test:
output+=(l+" ")
print(output)