3

How would I separate characters once I have a string of unseparated words?

For example to translate "take the last character of the suffix and add a space to it every time ("aSuffixbSuffixcSuffix" --> "aSuffix bSuffix cSuffix").`

Or, more generally, to replace the x-nth character, where x is any integer (e.g., to replace the 3rd, 6th, 9th, etc. character some something I choose).

3 Answers 3

2

Assuming you're getting your string from this question, the easiest way is to not cram all the strings together in the first place. Your original create_word could be changed to this:

def create_word(suffix):
    letters="abcdefghijklmnopqrstuvwxyz"
    return [i + suffix for i in letters]

Then you'd be able to take e and ''.join(e) or ' '.join(e) to get the strings you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you mean to miss out the letter d from letters?
@Luke Probably not. I copied that line from the question in the link, but fixed anyway.
2
   str = "SuffixbSuffixcSuffix"

   def chunk_str(str, chunk_size):
        return [str[i:i+chunk_size] for i in range(0, len(str), chunk_size)]

   " ".join(chunk_str(str,3))

returns:

'Suf fix bSu ffi xcS uff ix'

Comments

1

You could use the replace method of strings. Check the documentation

initial = "aSuffixbSuffixcSuffix"

final = initial.replace("Suffix", "Suffix ")
print(final)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.