Nov-25-2017, 02:55 AM
in Python source code, strings can be written with certain backslash codes like
what i would like to know is how to do the same conversions as the Python interpreter will do when processing strings like that in source code, such as if you Python code needs to read in some Python source code an do the same with a string it gets. also, how would you do the reverse, such as the need to convert a string with things like a newline and a carriage return in them, to a string ready to be placed between quotes and output as part of some Python source?
i can already think of ways to do this. but i am wanting to know how you would do it. i would like to see what is the most pythonic way or if there is a function in some module that will just do it. code that can handle bytes and bytearray, working under python3, would be even better. bode that can do all under both python2 and python3 would be best.
'foo\nbar'
will have a len() of 7 and print with 'foo' and 'bar' split to separate lines. ord('foo\nbar'[3])
and ord('\n')
will both be 10.what i would like to know is how to do the same conversions as the Python interpreter will do when processing strings like that in source code, such as if you Python code needs to read in some Python source code an do the same with a string it gets. also, how would you do the reverse, such as the need to convert a string with things like a newline and a carriage return in them, to a string ready to be placed between quotes and output as part of some Python source?
i can already think of ways to do this. but i am wanting to know how you would do it. i would like to see what is the most pythonic way or if there is a function in some module that will just do it. code that can handle bytes and bytearray, working under python3, would be even better. bode that can do all under both python2 and python3 would be best.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.