For example, if I entered I love dogs
, it would need to look like this:
I l o v e d o g s
This code does not do what I need it to do:
def spaceitout(source):
pile = ""
for letter in source:
pile = pile+letter
print pile
print pile
def evenly_spaced(string_,space_= 1):
import re
return (' '*space_).join([c for c in re.split(r'(\w)',string_) if c.isalpha()])
print(evenly_spaced(" This a long story ",2))
T h i s a l o n g s t o r y
Does this do what you need?
pile = ' '.join(source)
This takes the elements of "source" and joins them with a single space as the connector.
If you need only the letters separated, then build the list of letters only, and join that:
pile = ' '.join([c for c in source if c.isalpha()])
Spaces between letters:
def spaceitout(source, count):
return (' '*count).join([letter for letter in source.replace(' ','')])
Spaces between words:
def spaceitout(source, count):
return (' '*count).join(source.split())
Spaces between all chars:
def spaceitout(source, count):
return (''.join([c + (' '*count) for c in source]).strip())
Simple answer would be:
def spaceitout(source):
pile = ""
for letter in source:
pile = pile + letter + " "
pile = pile[:-1] #Strip last extraneous space.
print pile
Allows you to specify spaces between words, and spaces between characters. Based on the answers provided by BigOldTree
def space_it(text, word_space=1, char_space=0):
return (' '*word_space).join([(' '*char_space).join(x) for x in text.split(' ')])
Note: This will treat two spaces in the input text as having an "invisible word" between them, change text.split(' ')
to text.split()
if this isn't desired.
i think that this is what you was looking for:
line = 'I love dogs'
for i in line:
if i != ' ':
print i,
else:
print '',
using itertools:
import itertools
def space_word(word, spaces_count=1):
if spaces_count < 1:
raise ValueError("spaces_count < 1")
def space_word_wrapped(word, spaces_count):
letters = itertools.chain.from_iterable(zip(word, itertools.cycle(" ")))
skipped = 0 # have to keep track of how many letter skips
# or else from_word starts breaking!
# TODO : better implementation of this
for i, lett in enumerate(letters, start=1):
from_word = (i + skipped) % 2
if lett.isspace() and from_word:
if spaces_count == 1:
next(letters)
skipped += 1
continue # and skip the space itself
elif spaces_count == 2:
continue # just count the space
elif spaces_count == 3:
pass # count everything
else: # spaces_count >= 4
yield from [" "] * (spaces_count - 3)
yield lett
return ''.join(space_word_wrapped(word, spaces_count)).rstrip()
It's probably cheaper to just use an iterator here, but I like the generator in a nested function approach. So sue me! :)
This lists your word( 'his' = ['h','i','s'] and then joins it with a space instead of a comma.
def space(word):
return ' '.join(list(word))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.