Python Exercises: Insert space before capital letters in word
Insert space before capital letters.
Write a Python program to insert space before every capital letter appears in a given word.
Sample Data:
("PythonExercises")  -> "Python Exercises"
("Python")  ->  "Python"
(“PythonExercisesPracticeSolution”)  -> “Python Exercises Practice Solution”
Sample Solution:
Python Code:
# Define a function to insert a space before capital letters in a given word
def test(word):
    result = ""
    # Iterate through each character in the word
    for i in word:
        # Check if the character is uppercase
        if i.isupper():
            # Concatenate a space and the uppercase version of the character to the result
            result = result + " " + i.upper()
        else:
            # Concatenate the character to the result
            result = result + i
    # Remove the leading space from the result and return
    return result[1:]
# Initialize a string representing a word
word = "PythonExercises"
# Print the original word
print("Original Word:", word)
# Print a message indicating the insertion of space before capital letters
print("Insert space before capital letters in the said word:")
# Call the function to modify the word and print the result
print(test(word))
# Repeat the process with a different word
word = "Python"
# Print the original word
print("\nOriginal Word:", word)
# Print a message indicating the insertion of space before capital letters
print("Insert space before capital letters in the said word:")
# Call the function to modify the word and print the result
print(test(word))
# Repeat the process with a different word
word = "PythonExercisesPracticeSolution"
# Print the original word
print("\nOriginal Word:", word)
# Print a message indicating the insertion of space before capital letters
print("Insert space before capital letters in the said word:")
# Call the function to modify the word and print the result
print(test(word)) 
Sample Output:
Original Word: PythonExercises Insert space before capital letters in the said word: Python Exercises Original Word: Python Insert space before capital letters in the said word: Python Original Word: PythonExercisesPracticeSolution Insert space before capital letters in the said word: Python Exercises Practice Solution
Flowchart:
 
For more Practice: Solve these Related Problems:
- Write a Python program to insert a space before each capital letter in a CamelCase string using regular expressions.
- Write a Python program to iterate through a string and insert a space before each uppercase letter, except the first character.
- Write a Python program to use re.sub() to add spaces before capital letters and then trim any leading spaces.
- Write a Python program to implement a function that transforms a concatenated string of words into a space-separated sentence based on capitalization.
Go to:
Previous Python Exercise: Count the number of leap years within the range. 
Next Python Exercise: Alphabet position in a string.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
