20

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.

6
  • I know how %paste works. This was not the question, and I wonder if you read the question at all, before you downvoted it oO Commented Oct 8, 2012 at 19:15
  • If you open the triple quotes and then paste from the clipboard, there shouldn't be any error messages. Commented Oct 8, 2012 at 19:26
  • 2
    No, this just gives a "%paste" as a string, not the clipboard. Commented Oct 8, 2012 at 20:26
  • I mean literal paste (Ctrl+V or whatever). Commented Oct 9, 2012 at 7:16
  • 1
    Open triple quotes and then using ctrl+v is another valid option, still %paste strvar is nicer, as it doesn't clutter your console with megabytes of buffer content. Using %paste strvar for a while now, I use quotes+ctrl+v only, when %paste strvar sometimes drops some weird parsing error about the content. Commented Feb 21, 2013 at 5:23

1 Answer 1

32
%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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's actually a special list subclass - you can do strvar.n to get newline-joined text. Likewise strvar.s for space-separated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.