S
S
SpartakGusev2022-01-06 17:23:08
Python
SpartakGusev, 2022-01-06 17:23:08

How is the result of displaying a variable through the print() function different from accessing it directly?

Actually the whole question is described in the title
I do not understand the difference.
I see that in dictionaries with a long value, the output result is slightly different
61d6fb2b06054875326973.png
61d6fb23f102c522080023.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2022-01-06
@SpartakGusev

Some development environments allow you to display the contents of a variable. For example, Jupyter or an interactive python interpreter. But this is not a python feature, it is a development environment feature. Simplifying, for each expression, the environment executes something like this:

result = введённое_тобой_выражение
if result is not None:
    print(repr(result))

And here are ways to convert an object to a string in python and indeed 2.
str(some_object) should give a human-readable representation.
repr(some_object) should (if possible) give a representation that describes that object in python syntax.
A good example is strings.
print(str("foo\nbar")) will print
foo
bar

Whereas print(repr("foo\nbar")) will print
"foo\nbar"

But for many objects there is neither a smart str() nor a smart repr(), so the two representations are the same.

K
kapp1, 2022-01-06
@kapp1

Apparently this is Jupiter and Ipynb's notebook.
And you try to output through the command line without a print) Or in the standard python IDE. We will see)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question