avatar Bite 324. Pretty string

Write a function that returns a neatly formatted string representation of a python object.

As you might have guessed, there is a package in the standard library that can help you with pretty printing and pretty formatting (pprint). This package can also be really useful for debugging with print statements.

The function pretty_str should pretty format the object with the following features:

 - Line breaks after items when 60 characters are reached

 - With nested items, only show items up to a depth of 2, otherwise display an ellipsis [...]

 - Dictionaries should be sorted according to their key order

>>> d={"Z": "Z"*40,
       "B": [1,[2,[3]]],
       "A": "A"*40}
>>> result = pretty_str(d)
>>> print(result)
 'B': [1, [...]],
Login and get coding
go back Beginner level
Bitecoin 2X

115 out of 127 users completed this Bite.
Will you be the 116th person to crack this Bite?
Resolution time: ~33 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 2.0 on a 1-10 difficulty scale.
» You can do it! 😌

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help