In Python, you can pretty-print objects such as lists Show
This article describes the following contents.
The textwrap module is useful to wrap or truncate long strings instead of lists or dictionaries. See the following article.
In the sample code, the pprint module is imported as follows, and the list of dictionaries is used as an example. The pprint module is included in the standard library, so it is unnecessary to install it additionally.
Basic usage of pprintThe normal
With
The line break position is determined by the argument settings described below. Note that, as shown in the example above, the elements of the dictionary are sorted in the order of the keys. If the parameter If you want to convert it to a string Specify the output width (number of characters): widthThe output width (number of characters) can be specified with The line is broken to fit the specified number of characters. The default value is
If
A line is broken at an element of a list or a
dictionary, not between Note that strings may be broken into a new line for each word.
Specify the output depth: depthThe output depth can be specified with Elements nested deeper than the
specified value are printed with the ellipsis
The default value is You can specify both
Specify the indent width: indentThe indent width can be
specified with
Reduce line breaks: compactBy default, all elements of a list or dictionary break lines if they do not fit into
If
Note that Convert to string: pprint.pformat()Dictionaries and lists can be converted to the string with
You can use
The parameters of
Example: Pretty-print a list of listsA list of lists is hard to read with
As mentioned above, where to start a new line is determined by the number of characters specified by If the number of elements is small, it fits into the default output width
If an appropriate
If you want to get it as a string, use
How do you print a list in Python format?How to print a list using a custom format in Python. a_list = [2, 4, 6, 8]. string_list = ["%i: %s" % (index, value) for index, value in enumerate(a_list)]. print(string_list). formatted_string = "\n". join(string_list). print(formatted_string). How do you print a simple list in Python?3 Easy Methods to Print a Python List. Using map() function to print a Python List. Python map() function can be clubbed with join() function to print a Python list easily. ... . Using '*' symbol to print a Python list. Next, we will be using Python '*' symbol to print a list. ... . Naïve Method- Using for loop.. How do I print a list like a string in Python?Convert a list to a string for display : If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.
How do you print a list without spaces in Python?Print the values in a List without Spaces in Python #. Use the str. join() method to join the list into a string.. If the list contains numbers, convert them to strings.. Use the print() function to print the string.. |