'Check if character in string has space behind

I am trying to write a function that breaks up camel casing by using a space between words. How do I check if char already has space behind?

def solution(s):
    space = ' '
    for chr in s:
        if chr.isupper() == True:
            new_str = s.replace(chr, space + chr)
    return new_str

Input:

"camelCaseWord"      # a word in camelCasing

Output:

"camel Case Word"    # separated by spaces where word starts with capital leter

My solution only gives me "camelCase Word"



Solution 1:[1]

How about this one? I used enumerate to get the index of iteration.

def solution(s):
    space = ' '
    space_positions = []
    for index, chr in enumerate(s):
        print(chr)
        if chr != space and chr.isupper() == True:
            if s[index - 1] != space:
                space_positions.insert(0, index)
    for index in space_positions:
        s = s[:index] + " " + s[index:]
    return s

Hope it could help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Lu