Sitelet https://web.archive.org/web/20240805203238/https://github.com/python/cpython/issues/109443
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print string instead of escaped unicode for some unprintable unicode #109443

Closed
NewUserHa opened this issue Sep 15, 2023 · 9 comments
Closed

Print string instead of escaped unicode for some unprintable unicode #109443

NewUserHa opened this issue Sep 15, 2023 · 9 comments
Labels
type-feature A feature request or enhancement

Comments

@NewUserHa
Copy link
Contributor

NewUserHa commented Sep 15, 2023 •

Feature or enhancement

Proposal:

'\u3000' # returns '\u3000'
{'key':'\u3000'} # reutrns {'key':'\u3000'}
print('\u3000') # returns " "
print({'key':'\u3000'}) # returns {'key': '\u3000'}

'\ue011' # returns '\ue011'
print('\ue011') # returns 
print({'key': '\ue011'}) # returns {'key': '\ue011'}

I need to copy the   or  to other programs for searching and etc.
but python seems to always escape some unprintable characters, even set sys.displayhook(None).

The print seems to work, but it's inconvenient to iterate over every object for a nested object to print.
The pprint seems not to work at all.

The default str seems not easy to modify. And I haven't found any document about this.
I wonder how to change this behavior of Python on demand.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

@NewUserHa NewUserHa added the type-feature A feature request or enhancement label Sep 15, 2023
@ericvsmith
Copy link
Member

This is the difference between repr(some_string) and str(some_string). This isn't a bug in Python, and it's not clear what you're trying to do, so this isn't the correct forum. I suggest asking your question on https://discuss.python.org/c/users/7. You'll need to describe in more detail what you're trying to do, and what is or isn't working for you.

@ericvsmith ericvsmith closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2023
@NewUserHa
Copy link
Contributor Author

NewUserHa commented Sep 15, 2023 •

There's a need that copy unicode strings to other programs for future processes, so python is there to print those characters.

Neither repr(some_string) nor str(some_string) are related to this issue. And no document mentioned how sys.stdout.write escapes those unicode automatically.
It seems that Python lacks this feature, and I didn't report this as a bug.

seems currently Python has no ability to print objects containing uncommon Unicode strings without escaping them, except manually iterate over every inside object and manually print it.
So I opened this issue to discuss.

@NewUserHa
Copy link
Contributor Author

NewUserHa commented Sep 15, 2023 •

It may be a special and less common use case,
but I think at least it/the auto-escape-unicode behavior can be documented (like in https://docs.python.org/3/howto/unicode.html or blow the document of print).

@ericvsmith
Copy link
Member

print() is printing the str() of the string (which is just the string itself. Inside other data structures, such as the dict you show, the repr() of the string is used.

You have not described how your feature would work. Please do so.

@NewUserHa
Copy link
Contributor Author

print() is printing the str() of the string (which is just the string itself. Inside other data structures, such as the dict you show, the repr() of the string is used.

print() does print the str() or __str()__ of the string. But there are differences:

In [1]: print('\ue011')

In [2]: str('\ue011')
Out[2]: '\ue011'
In [3]: print({'\ue011'})
{'\ue011'} # it's still str(), not repr() inside the set or dict.
In [4]: repr('\ue011')
Out[4]: "'\\ue011'"

so now the only way to print those characters in Python is print with the literal string directly. (can not be done if the string happens in any object form).
even with json.dumps, it still prints the '\ue011'. (and you have to write it to a file and then open it with 3rd part editor to get the real characters. In other words, python can't print unicode characters inside objects easily.)

it's very helpful displaying escaped Unicode point in programing, but sometimes ppl want print the real character as well like font processing.
I think, there could be a switch to turn off the 'auto escape unicode in print' or something else.

@ericvsmith
Copy link
Member

Some of those are being printed by print, some are being printed by your REPL (Jupyter?). This all seems to be working as expected. I suspect you're looking for a Jupyter feature, you might want to ask there.

@NewUserHa
Copy link
Contributor Author

NewUserHa commented Sep 15, 2023 •

You can just verify it by yourself.

It's the same result, no matter in pure python REPL, or run with python .py, or in jupyter.
for now Python just can't print those characters in objects.

@terryjreedy
Copy link
Member

Interactive CPython and anything the imitates it prints the repl() of the object, even if it is a string. Use print() for the str(). Whether an unprintable character shows as a box or blank depends on the OS, display device, and maybe the current font, not python.

@NewUserHa
Copy link
Contributor Author

Interactive CPython and anything the imitates it prints the repl() of the object, even if it is a string. Use print() for the str(). Whether an unprintable character shows as a box or blank depends on the OS, display device, and maybe the current font, not python.

Right. The use case intends to print a box or blank, but cPython only can print the escaped Unicode code(e.g. '\u3000' not ' ') if the string is not directly as argument passed to the 'print' (e.g. say in a object), even with 'python test.py' instead of repl env.

# python test.py
print('\u3000')
print({'\u3000'})

prints

 
{'\u3000'}

rather than the expected (with a new option)

 
{' '}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants