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.
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).
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.
If you want to execute Python statements, you can use exec(string). For example, >>> my_code = 'print "Hello World!"' >>> exec(my_code) Hello World!
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 .
%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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With