how to use python -c on windows?

somewheve :

https://docs.python.org/3/using/cmdline.html

This is the option documentation. but it doesn't provide me any useful message

I want to execute code in this way

python -c "def hello():\n    print('hello world')"

the error message

PS C:\Users\Administrator> python -c "def hello():\n    print('hello world')"
  File "<string>", line 1
    def hello():\n    print('hello world')
                                         ^
SyntaxError: unexpected character after line continuation character

it works on Linux, but not windows.

Hope you can give a full fixed command ~~~

Another question connected with this one \n problem when using javascript to exec "python -c "

Heap Overflow :

Try backtick instead of backslash.

Error:

PS C:\Users\me> python -c "def hello():\n    print('hello world')"
  File "<string>", line 1
    def hello():\n    print('hello world')
                                         ^
SyntaxError: unexpected character after line continuation character
PS C:\Users\me>

Ok:

PS C:\Users\me> python -c "def hello():`n    print('hello world')"
PS C:\Users\me>

Useful:

PS C:\Users\me> python -c "def hello():`n    print('hello world')`nhello()"
hello world
PS C:\Users\me>

Just echoing to see it:

PS C:\Users\me> echo "def hello():`n    print('hello world')`nhello()"
def hello():
    print('hello world')
hello()
PS C:\Users\me>

See PowerTip: New Lines with PowerShell

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21190&siteId=1