Using f-strings:
plot.savefig(f'hanning{num}.pdf')
This was added in 3.6 and is the new preferred way.
Using str.format():
plot.savefig('hanning{0}.pdf'.format(num))
String concatenation:
plot.savefig('hanning' + str(num) + '.pdf')
Conversion Specifier:
plot.savefig('hanning%s.pdf' % num)
Using local variable names (neat trick):
plot.savefig('hanning%(num)s.pdf' % locals())
Using string.Template:
plot.savefig(string.Template('hanning${num}.pdf').substitute(locals()))
See also:
- Fancier Output Formatting - The Python Tutorial
- Python 3's f-Strings: An Improved String Formatting Syntax (Guide) - RealPython
Using f-strings:
plot.savefig(f'hanning{num}.pdf')
This was added in 3.6 and is the new preferred way.
Using str.format():
plot.savefig('hanning{0}.pdf'.format(num))
String concatenation:
plot.savefig('hanning' + str(num) + '.pdf')
Conversion Specifier:
plot.savefig('hanning%s.pdf' % num)
Using local variable names (neat trick):
plot.savefig('hanning%(num)s.pdf' % locals())
Using string.Template:
plot.savefig(string.Template('hanning${num}.pdf').substitute(locals()))
See also:
- Fancier Output Formatting - The Python Tutorial
- Python 3's f-Strings: An Improved String Formatting Syntax (Guide) - RealPython
With the introduction of formatted string literals ("f-strings" for short) in Python 3.6, it is now possible to write this with a briefer syntax:
>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
With the example given in the question, it would look like this
plot.savefig(f'hanning{num}.pdf')
ssh - How to pass string from bash to python function - Unix & Linux Stack Exchange
how to pass variable values to a string in python - Stack Overflow
How to pass string as variable in Python? - Stack Overflow
How to insert a variable (string) in middle of URL
Videos
Make it a shell function instead. Paste this into your terminal, or add to your shell's configuration files (e.g. ~/.bashrc for bash)
pipeline_test(){
python -c 'from sys import argv; from pipeline_runs.z_full_pipeline import test; test(argv[1])' "$@"
}
And then run as
pipeline_test "$a"
Just try:
export a="test"
python -c "from pipeline_runs.z_full_pipeline import test; test('$a')"
In this case, the command will be expanded to from pipeline_runs.z_full_pipeline import test; test('test')
And you will have 'test' instead of test. The former is a string and the later is an undefined variable.
But a safe way to proceed is to use the os.environ dictionary. Then you put a script in a example.py file. And start with:
import os
a = os.environ['a']
test(a)
This way is better scalable and you don't have to expand a string.
Note, in shell, a="test" and a=test are synonymous.
There are multiple methods to do that.:
fstring
data_file_format = f'{sourcesystem}_{batchid}_{extractname}_{loadtype}'
or using .format
data_file_format = '{}_{}_{}_{}'.format(sourcetype,batchid,extractname,loadtype)
sourcesystem ="xyz"
batchid = "101"
extractname = "abc"
loadtype = "Delta"
data_file_format="_".join([sourcesystem,batchid,extractname,loadtype])
#or
#data_file_format=sourcesystem+'_'+batchid +'_'+extractname+'_'+loadtype
print(data_file_format)
Getting my feet wet with python. Did automate the boring stuff and am now exploring how to use API's with Python.
What I am trying to do, is request passive DNS data from Threatminer based on domainname. The API documentation (https://www.threatminer.org/api.php) states that you can use the rt=2 flag for this. Example they give with the vwrm.com domain: https://api.threatminer.org/v2/domain.php?q=vwrm.com&rt=2
So I thought this would be easy. I thought just creating a variable and feeding it to the URL would be sufficient:
domain = input() domainoutput = requests.get( "https://api.threatminer.org/v2/domain.php?q="+domain+"rt=1") print(domainoutput.text)
However, I keep getting the response:
{"status_code":"404","status_message":"No results found.","results":[]} To me, this signals that the request was sent, but that no results were found. However, I have the sneaky suspicion that the actual domain that is entered in input is not correctly fed into the url. I am tripping up somewhere, but no idea where. Can someone tell me how to fix this/where to read up on how to fix this?
So I have a function to find max and min from an array.
function extremeValues(values,"Max","Min) I know how to find max and min but how to assign max and min to the value since it is a string?