PDA

View Full Version : [SOLVED] Python- Help on a question (tabstop)



Yozuru
April 6th, 2014, 05:21 PM
So I am doing practice problems I found online for python. The question goes as follow:

"Proceed to write a function called detab(string, stop) where string is any string, and stop is a positive integer called tabstop (size of it). It should return string, expect that each tab in the string has been replace with the number of underscores '_'. The only input function is the arguments, and the only output returns the string that has been detabbed. Do not use input nor print for this practice problem."


I have trouble in starting this code and have look for ways on how to do this. I am hoping I can get some input from the ubuntul community about this. Its a rather interesting problem and it would help improve my proficiency with python. Many thanks in advance.


I was thinking of something like:

string 1 = string
return string1.expand(stop)

steeldriver
April 6th, 2014, 05:47 PM
Your starting point should be the python string library documentation --> https://docs.python.org/2/library/string.html

You will also need to think how you can construct the replacement string (a string of underscores) programatically from an integer (the number of underscores)

Vaphell
April 6th, 2014, 07:50 PM
You will also need to think how you can construct the replacement string (a string of underscores) programatically from an integer (the number of underscores)

well, luckily for the OP, there is nothing to think about. In python the * operator does that by default

Yozuru
April 10th, 2014, 07:26 PM
Ahh interesting! I forgot about list unpacking. :) Thanks guys.