Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 364
Michael Avatar asked Oct 08 '12 18:10

Michael


People also ask

How do you paste into IPython?

To copy text, just select it and hit Ctrl-C (Command-C on a Mac). If the highlight marking the selection disappears, that's normal and it means it's worked. To paste, use Ctrl-V (Command-V on a Mac).

How do you use a string name as a variable in Python?

String Into Variable Name in Python Using the vars() Function. Instead of using the locals() and the globals() function to convert a string to a variable name in python, we can also use the vars() function. The vars() function, when executed in the global scope, behaves just like the globals() function.

How do you code a string in Python?

If you want to execute Python statements, you can use exec(string). For example, >>> my_code = 'print "Hello World!"' >>> exec(my_code) Hello World!

How do you get a string variable in Python?

To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial. For example, you can assign a character 'a' to a variable single_quote_character .


1 Answers

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

like image 80
Daniel Avatar answered Sep 21 '22 17:09

Daniel