Spaces:
Sleeping
Sleeping
File size: 989 Bytes
00b1038 b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc 00b1038 b73a4fc 00b1038 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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}'")
|