# String column formatter, formats a String into any width, breaking only on spaces # Author: Dan Fleck def reformatString(inputStr, cols): strIndex = 0 #for i in range(10000): while (True): # Exit the loop if the String is not long enough remainder = len(inputStr) - strIndex if remainder < cols: break # Find the first instance of a space behind my strIndex strIndex = inputStr.rfind(' ', strIndex , strIndex+cols) if strIndex == -1: # We never found one return inputStr # Insert a newline there inputStr = inputStr[0:strIndex] + '\n' + inputStr[strIndex+1:] return inputStr def main(): print reformatString('Personal firewall software may warn about the connection IDLE makes to its subprocess using this computers internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet.', 40) main()