How to enter escape character in a positional string argument from the command line?
Mark Bourne
nntp.mbourne at spamgourmet.com
Mon Dec 19 15:48:49 EST 2022
Jach Feng wrote:
> I have a script using the argparse module. I want to enter the string "step\x0A" as one of its positional arguments. I expect this string has a length of 5, but it gives 8. Obviously the escape character didn't function correctly. How to do it?
That depends on the command-line shell you're calling your script from.
In bash, you can include a newline in a quoted string:
./your_script 'step
'
(the closing quote is on the next line)
Or if you want to do it on a single line (or use other escape
sequences), you can use e.g.:
./your_script $'step\x0a'
(dollar sign before a single-quoted string which contains escape sequences)
--
Mark.
More information about the Python-list
mailing list