Search code examples
pythoncopy-pasteipythonmagic-methods

Paste a string as a variable, not as executable code snippet, into IPython


I'm aware of the magic IPython %paste command, which is quite useful, if you have valid code to insert. Now I don't want to insert code, I just want to store some string from the copy buffer as a variable. Is there a simpler way to do that, except copying the string to some separate text editor and modifying it first?

Something like this would be nice, but none of them are working:

strvar = %paste
strvar = """%paste"""

P.S. The string is lengthy and contains special chars etc. so simple ctrl-c just creates garbage and error messages.


Solution

  • %paste strvar
    

    gives you a list of the lines from the copied text. You can do

    strvar = '\n'.join(strvar)
    

    to get the text in a single string.