Python-String to function in 'eval' - string

I'm trying to build a lisp interpreter in python using 'eval'.
def FUNC(e1,*elements):
print(e1)
eval((input()))
Its working for integers.But is there any way I can use this to input strings?
eg: when I input:
F(a,b,3,5,2)
The above code should print:
a
I don't want to input it as: F('a','b',3,5,2)
Thanks!

The only way to convey a string value is by enclosing it within quote. Other way to do it is by declaring a string variable separately and send it as a parameter instead. However, when you print a string query in interactive command window, it won't be printed with quotes aroun

Related

How to remove white spaces and capitalize every first letter in the string in the robotramework

How can I remove white spaces and capitalize every first letter in the string in the robotframework, so later I use the result in the Selenium library calls?
Test to Unlock the Service Account:
Open Browser ${URL} ${Browser}
${string_1} = get text ${question_1}
${temp_answer} = set variable ${string_1}.title()
${answer}= evaluate ${string_1}.replace(" ","")
Input Text ${Answer_1} ${answer}
sleep 5s
Input:
Legal business name
Output:
LegalBusinessName?
You were close to achieving it, but made two crucial mistakes. The first one is you used Set Variable and tried calling the python's title() string method in the argument - but that doesn't work for the keyword. It is a straightforward assignment - synonymous to the = operator; so what you ended up with as value was the string "Legal business name.title()". You should use the Evaluate keyword like in the second call, which does python's code eval.
The other mistake was to use two different variables - you store the capitalized version in the var ${temp_answer}, but then you don't remove the whitespace from it, but from the original one - ${string_1}. So even if the capitalization worked, you still wouldn't get the desired end result in the ${answer} var.
Here's a one-liner how to achieve what you need:
${answer}= evaluate """${string_1}""".title().replace(" ","")
The 2 methods are chained - replace() works on the result of title(), and the value of string_1 is in triple quotes so python works with its sting representation.

Why does this python code produce an error:?

Basically in python 3.0 I tried to use two %s string substitutes and concatenate it. However, it seems to produce an error.
CODE
print "%s"+"%s" %("John", "rows")
I am new to programming so I would be thankful if I could get a simple explanation.
Thanks
In python-2.x, which appears to be the language that line of code was written in (you treated print as a statement), you will get an error TypeError: not all arguments converted during string formatting.
This is because the interpreter attempts to put both of the arguments into the latter string, due to the order of operations. Thus, simply wrap the string with parentheses and the code will run:
>>> print ("%s"+"%s") %("John", "rows")
Johnrows
>>>

How to put triple qoutes around existing string variable?

Let's say I have this variable:
st='MI'
and I want to convert it to:
st=''' 'MI' '''
to use it in a SQL command.
What's the best way to accomplish this?
Thanks in advance!
Tripleor single quoting are just ways of typing strings into source code files.
Once your program is running, your string is already a string, and there is no need to make any cnversion to use it as a parameter to a SQL driver function call.
What you may want i to have a string with an SQL statement that itself contains various (single or double) quote characters. If that is typed in your Python source code file, you can type the triple-quote straight. If you are getting these SQL statements from elsewhere, they are already strings, as I said above.
Now, there are a few instances in which you have a string in a running Python program, or a Python interactive session, that you would like printed, so that you can paste it directly in source code. For these cases you can try the "unicode_escape" codec (and recode it to text so that it does not double your backslashes:
In [56]: print("\n".encode("unicode_escape").decode("utf-8"))
\n

Lua: returning or storing REPL/loadstring string output in a string variable

The same question already asked here
Lua: Executing a string and storing the output of the command in a variable
But I want the function loadstring somehow return the result in string form that may be assigned to a string variable later to use further, what it returns is a function.
Below code is an example:
ret = assert(loadstring(str_cmd))()
ret is a function:(
How can I get the REPL/loadstring output in string form?
If all output from str_cmd comes from calls to print, redefine print before loading str_cmd to save all output in a table. After running str_cmd, use the table in table.concat for instance.
Instead of redefining print you can provide an environment to run str_cmd in. You'll probably need to make that environment inherit from the global environment.

Passing List of Variables In Bash To External Program

Good Afternoon Everyone,
This is probably a no-brainer but, I'm currently having issues passing a variable to a program in my bash script.
Here's what I'm trying to do:
regions=ne,se,vt,ma,sw,nw and so on and so forth
After that variable has been defined in my bash script, I'd then like to pass that variable into GrADS, where my script will read each of the regions one after the other until it reaches the end.
The most reliable means of passing variables I've found is to generate a text file with the code (or just the string) you want to pass from within the code. Alternatively, you could call GrADS (?) from within whatever program is generating the variable, and pass "$regions" as an argument.

Resources