0

To process my data files I need whitespaces between all characters in a string, to be written to a text file. Is there a simple Pythonfunction to solve this?

2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by Steko
0

Hi!

Well, Python doesn't have one simple function for this, but you can combine 2 simple functions, list() and join() :)

s = "hello"
# convert the string to a list of characters
lst = list(s)    # lst now looks like this: ['h', 'e', 'l', 'l', 'o']
# now combine the list-elements to a string with a whitespace between them
new = ' '.join(lst)

Or shorter:

s = "hello"
new = ' '.join(list(s))

Regards, mawe

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.