Python check if a variable appears in another variable [duplicate] - python

This question already has answers here:
Python efficient way to check if very large string contains a substring
(8 answers)
Closed 1 year ago.
I'm a beginner to Python. I'm using the request module to get the text from a website that contains blacklisted users for the login system of my program. I want to know how to check if a variable appears in another variable such as, "if variable appears in variable2: do something"
Can anyone help? Thanks.

You can check that using the in keyword -
if object1 in object2:
#do something

Share your code. It would give a better understanding of what you need to do. I think the below code will work.
import requests
x = requests.get('https://yourwebsite.com')
if variable in x.text:
#do something

Related

Using a module with variable name in Python [duplicate]

This question already has answers here:
How to convert string to variable name?
(3 answers)
Closed 1 year ago.
I want to run a function of a module with a variable name for example:
Plugins.SomeUnknownName.function() without knowing their name but Plugins. and .function() stay the same.
I have tried several things like plugin = 'Plugins.' + unknown_plugin_name + '.function()' or
plugin_name = "SomePlugin"
Plugins.plugin_name.function()
But I don't know any further.
Maybe someone knows how to solve that.
Also this is my first stack overflow post, so please correct me if I did something wrong.
You can use getattr to pass the string name. Using numpy as an example here's what the normal usage would look like
>>> import numpy
>>> numpy.random.randint(1, 5)
2
Now similar to your example
>>> getattr(numpy, 'random').randint(1, 5)
2
So applying to your case
getattr(Plugins, 'plugin_name').function()

Injecting environment variables to string path [duplicate]

This question already has answers here:
How to evaluate environment variables into a string in Python?
(3 answers)
Closed 2 years ago.
I have the following string :
some_string = "%envvar1%\location\execution.exe"
envvar1 is an environment variable with value of "c:\", and I would like some function as following:
some_string = "%envvar1%\location\execution.exe"
inject_env_variable(some_string)
print(some_string)
"c:\location\execution.exe"
Creating a function like this wouldnt be to difficult with regular expressions and os.environ but I was wondering if there was some kind of built in module that treats these kind of things.
Note: google searching anything with the word 'path' and 'python' is really tedious since all the searches are related to pythonpath :P
os.path.expandvars is probably what you are looking for. https://docs.python.org/3/library/os.path.html#os.path.expandvars
import os
def inject_env_variable(s):
return s.replace("%envvar1%", os.environ['envvar1'])
Should do the trick

Python Passing list by its name to a function [duplicate]

This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 8 months ago.
names=['abcd','efgh']
nameoflist='names'
def(nameoflist=[]):
return nameoflist
I want to be able to return the entire list from the function
Assuming names is global as specified in the question, you can do this
names=['abcd','efgh']
nameoflist='names'
def return_names(nameoflist):
return globals()[nameoflist]
However, this is pretty ugly, and I'd probably try another way to do it. What do you need the name for? Is there any other way to get the information you're asking for?
This one way to do what you are asking. But it is not good programming.
names=['abcd','efgh']
def list_by_name(list_name):
return eval(list_name)
print(list_by_name('names'))
Also, argument list_name should be a string. It should not default to a list, which would make the function to fail if called without argument. It should not have a default value.

How do I use the python continue keyword as a string literal in a python script? [duplicate]

This question already has answers here:
Python Named Argument is Keyword?
(2 answers)
Closed 6 years ago.
I'm writing a python script wherein I need to use the continue word.
I know that it is a python keyword.
So how should I write my script so that python will not complain that I am using a keyword as a string literal.
Thanks in advance for your time.
You should use something like:
_continue
But why not using more descriptive and longer variable name?! like:
continue_whatever or go_on ...
The normal practice is to use a trailing underscore to use a keyword that already being used by the language, like continue_ or input_.
I don't think anyone would think that just overwriting keywords is every a good idea.

How would one create variables in a loop? [duplicate]

This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
I would like to know how to create lots of variables by looping it. I know other people have asked this before but everyone who knows says you need a good reason for it and to just set it in a dictionary. My reason is that I need to assign up to 6156119580207157310796674288400203776 variables and there is no way I can do that by typing them out.
I need something like:
while counter < 1000:
try[counter] = counter
So that I could do this:
>>> try837
837
>>>try453
453
etc.
(this is an example not the exact code but any answer for this will solve my problem)
I would also like to know why people are opposed to answering this particular question. I don't want to tax my computer more than I already am by assigning this many variables so if it is an issue that could harm my computer or my code I would like to know.
You don't want to do this. Create a dictionary with a key for each suffix that you would use. Then use try[557] in place of the variable try557.
>>> try_ = dict((counter, counter) for counter in range(1000))
>>> print try_[557]
557
I'm using the standard technique of affixing an underscore to the otherwise reserved word "try".
(I'm ignoring the ludicrously large number of variables you claim to need.)

Categories

Resources