4

I am using the python module, markovify. I want to make new words instead of making new sentences.

How can I make a function return an output like this?

spacer('Hello, world!') # Should return 'H e l l o ,   w o r l d !'

I tried the following,

def spacer(text):
  for i in text:
    text = text.replace(i, i + ' ')
  return text

but it returned, 'H e l l o , w o r l d ! ' when I gave, 'Hello, world!'

2
  • this text has three L so it will replace L three times and this makes problem. You would have to get unique chars using set() like set(text) Commented May 7, 2022 at 15:51
  • You’d be probably better off using the markovify.Chain class directly instead of shoehorning individual words into a sentence-based model. Commented May 7, 2022 at 15:58

4 Answers 4

12
  1. You can use this one.
def spacer(string):
    return ' '.join(string)

print(spacer('Hello,World'))

  1. Or You can change this into.
def spacer(text):
  out = ''
  for i in text:
    out+=i+' '
  return out[:-1]

print(spacer("Hello, World"))
  1. (If you want) You could make the same function into a custom spacer function, But here you also need to pass how many spaces(Default 1) you want in between.
def spacer(string,space=1):
    return (space*' ').join(string)

print(spacer('Hello,World',space=1))
  1. OR FOR CUSTOM SPACES.
 
def spacer(text,space=1):
  out = ''
  for i in text:
    out+=i+' '*space
  return out[:-(space>0) or len(out)]

print(spacer("Hello, World",space=1))

OUTPUT

H e l l o ,   W o r l d
Sign up to request clarification or add additional context in comments.

14 Comments

indeed! but try your function without it! it works---slightly better, since ram is reduced :) (Strings are iterable in python, and str.join() takes any iterable. Building and then throwing away a list doesn't help here). Inded the reason list("sha") returns ['s', 'h', 'a'] is because strings are iterable---str.join() will iterate either in exactly the same way.
Thanks so much this seems to work quite well. :)
Indeed, strings are already iterables without converting them to list.
@SharimIqbal not so much a mistake---but a very common antipattern in python for some reason. 'nearly everything is iterable' makes python rather cool.
@2e0byo The join method itself still builds a list anyway. And it can save memory to do it yourself!
|
6

The simplest method is probably

' '.join(string)

Since replace works on every instance of a character, you can do

s = set(string)
if ' ' in s:
    string = string.replace(' ', '  ')
    s.remove(' ')
for c in s:
    string = string.replace(c, c + ' ')
if string:
    string = string[:-1]

The issue with your original attempt is that you have ox2 and lx3 in your string. Replacing all 'l' with 'l ' leads to l . Similarly for o .

4 Comments

Agreed, this is the method I would personally use.
Yes, looks correct now. Though also similar to but much more complicated than AlveMonke's.
@KellyBundy. The first version is how I'd normally do it. Alve's version is quite clever and has my endorsement. The second version here is to help OP understand their own attempt. It's basically an advertisement for why not to do it that way.
Ah, ok. Hadn't noticed the similarity to the OP's (not sure I had read theirs). And yes, join is certainly the nicest way.
5

The simplest answer to this question would be to use this:-

"Hello world".replace("", " ")[1:-1]

This code reads as follows:- Replace every empty substring with a space, and then trim off the leading and trailing spaces.

11 Comments

Not the simplest and also not the most efficient.
@LMD Definitely not the most efficient but if you're coding in python then this should be considered normal(simple in python).
@avm Yes, that is exactly what I meant by "the simplest answer".
@LMD What's more efficient?
@avm Same question to you, what's more efficient?
|
3
print(" ".join('Hello, world!'))

Output

H e l l o ,   w o r l d !

5 Comments

Literally a duplicate of MadPhysicist's answer
I didn't peek. If that's what you mean.
Given the simplicity of the problem, I believe you.
@MadPhysicist if you look at the other answers, there will also be duplicates. It is unclear why I was selectively voted minus because of this.
Sorry. Knee-jerk response :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.