Text Wrap

  • + 1 comment
    import textwrap
    
    def wrap(string, max_width):
        wrapper = textwrap.TextWrapper(width=max_width)
        
        return '\n'.join(wrapper.wrap(text = string))
    
    if __name__ == '__main__':
        string, max_width = input(), int(input())
        result = wrap(string, max_width)
        print(result)
    
    • + 2 comments

      can you explain the return statemen of your code?

      • + 0 comments

        I think it should return a string, for example, my_string = ''.join(my_list). This line of code means to concatenate all the elements in a list called my_list into a single string, and store the result in the my_string variable.

      • + 0 comments

        It's joining the charcters with a '\n' or enter after wrapping the string and returns the joined characters.