Sunday 15 March 2015

python - Preparing a string by concatenation and accessing its contents -


I want to create a string name with inserting two other small stars and then I want to use his content.

Example:

  teststring = "teststringlength" len ("test" + "string")   

Length of 'teststring', i.e. "test string lamp" length = 15

I do not know What to do with C, but in Python, you can do this:

  eval ('len ({} {})' format ('test', 'string')) < / Code>  

It is not very secure, even if a malicious user If able to provide an arbitrary string, it can run arbitrary python code.

Another option locals () :

  local () ['test' + 'string']   

There are some warnings for its use (see for more information; see also Gabe's comment below for possible improvement). / P>

Finally, the preferred solution is usually to use a dictionary instead of multiple variables:

  data = {'teststring': 'teststringlength'} len ( Data ['test' + 'string'])    

No comments:

Post a Comment