I'm trying to write a python function not using any modules that will take a string that has tabs and replace the tabs with spaces appropriate for an inputted tabstop size. It can't just replace all size-n tabs by n spaces though, since a tab could be 1 to n spaces. I'm really confused, so if anyone could just point me in the right direction I'd greatly appreciate it.
For instance, if tabstop is size 4 originally:
123\t123 = 123 123 #one space in between
but changed to tabstop 5:
123\t123 = 123 123 #two spaces in between
I think I need to pad the end of the string with spaces until string%n==0 and then chunk it, but I'm pretty lost at the moment..
I needed something similar, here's what I came up with:
This code can help you:
You will need to study: format, split and join function and list comprehension concept.
Sorry, i misread the question the first time.
This is a recursive version that should work for any number of tabs in the input :
I think Remi's answer is the simplest but it has a bug, it doesn't account for the case when you are already on a "tab stop" column. Tom Swirly pointed this out in the comments. Here's a tested fix to his suggestion:
Use the re.sub is enough.