2

I can exit a python 3 environment in powershell easily using Control c which gives

>>> ^Z

and pressing enter to exit.

If I instead type or paste ^Z and hit enter I get:

SyntaxError: invalid syntax

What is the difference?

Shouldn't text be treated the same regardless of how it's entered?

1 Answer 1

1

When you press CTRL + C, Python interprets it as a Substitute Character. This character is used by Python to denote the end of file. Python represents the substitute character by using ^Z because in many environments CTRL + Shift + Z can be used to send this character.

However, when you paste ^Z into python and hit Enter, it does not exit. This is because when python reads ^Z it gets three bytes, 0x5e 0x5a 0x0a, which represent ^, Z and New Line respectively. When you press CTRL + Shift + Z, Python gets only one byte, 0x1a, which represents the substitute character. Since the values are not the same, Python does not interpret them as the same.

The same result occurs with CTRL + C because this signals python to close. The way Python closes itself is by sending itself the substitute character, which is why you see ^Z on the command line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.