Interpreting \ escape sequences in strings

Peter Otten __peter__ at web.de
Sun Mar 14 02:29:16 EST 2004


Paul Watson wrote:

> I did have not explained it clearly.  I want the user to specify a string

Seems it was clear enough, you only didn't recognize the answer :-)

> that I will put between words in the output.  The user specified string
> can
> have escape sequences.  For example, the user wants to put a binary 1
> (\001) between each output word.
> 
> import sys
> words = ['now', 'is', 'the', 'time']
> print '\001'.join(words)        #this works
> print sys.argv[1].join(words)   #this fails

Change the above line to

print sys.argv[1].decode("string_escape")

s.decode("string_escape") returns a new string with all c-style escape
sequences converted into the corresponding characters. This is an abuse -
ahem, example of a general mechanism. Look for codecs if you want to learn
more about it.

Peter




More information about the Python-list mailing list