Spaces:
Sleeping
Sleeping
def replace_leading_spaces(text): | |
""" | |
Replaces leading spaces in a string with ' '. | |
Args: | |
text: The input string. | |
Returns: | |
The string with leading spaces replaced by ' '. | |
""" | |
leading_spaces = 0 | |
for char in text: | |
if char == " ": | |
leading_spaces += 1 | |
else: | |
break | |
if leading_spaces > 0: | |
return " " * leading_spaces + text[leading_spaces:] | |
else: | |
return text | |
# Example usage: | |
text1 = " Hello, world!" | |
text2 = "No leading spaces." | |
text3 = " Another example." | |
text4 = "\t Test with tabs" # this will not be replaced, only standard spaces | |
result1 = replace_leading_spaces(text1) | |
result2 = replace_leading_spaces(text2) | |
result3 = replace_leading_spaces(text3) | |
result4 = replace_leading_spaces(text4) | |
print(f"'{text1}' becomes '{result1}'") | |
print(f"'{text2}' becomes '{result2}'") | |
print(f"'{text3}' becomes '{result3}'") | |
print(f"'{text4}' becomes '{result4}'") | |