I think you're asking if you can call the function like this:

count(this is my string, i)  # 3

instead of like this:

count('this is my string', 'i')  # 3

And the answer is that there is no way to do this. The arguments are parsed when the source code is parsed and the source code parser would have no way to disambiguate a string with commas in it from multiple input arguments.

Of course, this is just one example of parser ambiguity. If a feature like that were to be proposed, I suppose that if you were really strict in the format of the input strings (no commas, extra syntax to disambiguate strings that have variable names from strings that didn't, etc) you could probably come up with a syntax to make it work. However, remembering all of that would be much more difficult than just enclosing your strings in quotes as they're meant to be (and it would make the python parser much more cumbersome to work with). So all in all, I would think it would be a net-loss for the language for them to introduce a feature like that (though I'm just one person who isn't the BDFL so don't mistake my opinion for the opinion of someone who matters).

Answer from mgilson on Stack Overflow
🌐
IncludeHelp
includehelp.com › python › passing-string-value-to-the-function.aspx
Python | Passing string value to the function
# function definition: it will accept # a string parameter and return number of vowels def countVowels(str): count = 0 for ch in str: if ch in "aeiouAEIOU": count += 1 return count # Main code # function calls str = "Hello world!" print('No. of vowels are {0} in "{1}"'.format(countVowels(str), str)) str = "Hi, I am good." print('No. of vowels are {0} in "{1}"'.format(countVowels(str), str)) ... No. of vowels are 3 in "Hello world!" No. of vowels are 5 in "Hi, I am good." In the above examples, we have used the following Python topics that you should learn:
Discussions

how to create a function takes a single argument, a string.
ali fadhil is having issues with: how to give functions argument like strings help ? More on teamtreehouse.com
🌐 teamtreehouse.com
3
June 28, 2017
Python - change string into arguments and pass it to a function - Stack Overflow
I am using python with a mysql db. I have a table in that db storing names of functions to be run. I would like to also add a possibility to store variables in the table, retrieve them on the fly and pass them into the function. However when stored in the db, the arguments are strings, and I ... More on stackoverflow.com
🌐 stackoverflow.com
Passing a string to a function
In the following code, basic_text = """ SystemName water SystemLabel water \n""" def basic(basic_text:str): siesta = basic_text siesta += "#\n# Definition of Atomic Species\n#\n" siesta += "#\n# Atoms\n#\n" siesta += "LatticeConstant \n" print(siesta) I am hoping that the following gets printed ... More on discuss.python.org
🌐 discuss.python.org
4
0
February 6, 2025
Use a string to call function in Python - Stack Overflow
Some days ago I was searching on the net and I found an interesting article about python dictionaries. It was about using the keys in the dictionary to call a function. In that article the author has More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › use an argument input as string inside a function to use as a string to print the input argument and its value
r/learnpython on Reddit: Use an argument input as string inside a function to use as a string to print the input argument and its value
April 4, 2023 -

I want to write a function that uses a given argument inside the same function in two ways:

as a string as a object, int or float, etc. the structure should be like this:

def func(any_given_argument): print('the "any_given_argument" value is:', any_given_argument)

Examples of desired result:

func(2) = 'the 2 value is:' 2

func(df['col1']) = "the value of df['col'] is:" value of df['col1'] series

func(2+4) = 'the value of 2+4 is:' 6

Need some help doing this because I've tried a lot but didn't succeed... thanks a lot in advance!!

🌐
Medium
medium.com › @jamesaaa100 › how-to-call-a-python-function-with-a-string-cdd788fd5d42
How To Call a Python Function With A String? | by John SS | Medium
August 2, 2024 - Here’s an example of how you can use eval() to call a function by its string name: ... In this example, we define a function greet() that takes a name as an argument and prints a greeting. We then store the string "greet" in the variable func_name.
🌐
W3Schools
w3schools.com › python › gloss_python_function_arguments.asp
Python Function Arguments
Python Functions Tutorial Function Call a Function *args Keyword Arguments **kwargs Default Parameter Value Passing a List as an Argument Function Return Value The pass Statement i Functions Function Recursion
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › passing-function-as-an-argument-in-python
Passing function as an argument in Python - GeeksforGeeks
March 1, 2025 - Basically, we use a function of any module as a string, let's say, we want to use randint() function of a random module, which takes 2 parameters [Start, End] and generates a random value between · 3 min read Explicitly define datatype in a Python function · Unlike other programming languages such as Java and C++, Python is a strongly, dynamically-typed language. This means that we do not have to explicitly specify the data type of function arguments ...
🌐
Python.org
discuss.python.org › python help
Passing a string to a function - Python Help - Discussions on Python.org
February 6, 2025 - In the following code, basic_text = """ SystemName water SystemLabel water \n""" def basic(basic_text:str): siesta = basic_text siesta += "#\n# Definition of Atomic Species\n#\n" siesta += "#\n# At…
🌐
Google Groups
groups.google.com › g › comp.lang.python › c › vgOWCZ7Z8Yw
converting a string to a function parameter
March 13, 2009 - Various namespaces will have dictionary-like associations between a name (like "m") and an object, and it *is* possible to get your hands on a (dictionary representing a) namespace and search it, but this is troublesome. For instance, consider this small variation of your code: def function(m): return takethenameof(m) a=[654,54,65] b = a function(a) While function is running, there will be three names associated with the list object.
🌐
Python
docs.python.org › 3 › c-api › arg.html
Parsing arguments and building values — Python 3.14.3 documentation
Currently, all keyword-only arguments must also be optional arguments, so | must always be specified before $ in the format string. Added in version 3.3. ... The list of format units ends here; the string after the colon is used as the function name in error messages (the “associated value” of the exception that PyArg_ParseTuple() raises).
🌐
Programiz
programiz.com › python-programming › function-argument
Python Function Arguments (With Examples)
Similarly, last_name in the function call is assigned to last_name in the function definition. In such scenarios, the position of arguments doesn't matter. Sometimes, we do not know in advance the number of arguments that will be passed into a function. To handle this kind of situation, we can use arbitrary arguments in Python.
🌐
Tutorialspoint
tutorialspoint.com › python › python_functions.htm
Python - Functions
The following example shows how to use keyword arguments in Python. # Function definition is here def printme( str ): "This prints a passed string into this function" print (str) return; # Now you can call printme function printme( str = "My string")
Top answer
1 of 6
3

I think this question boils down to the difference between named and positional arguments.

Let's define a function foo(bar):

def foo(bar):
    print(bar, "(type: " + str(type(bar)) + ")")

Now, let's run some examples:

foo(1)
# 1 (type: <class 'int'>)

This is a positional argument, it goes into the function in the order you give it. In this case, there is only one argument, so it goes into the first position of the foo function, which is the argument bar.

foo(bar=1)
# 1 (type: <class 'int'>)

This is the named equivalent of the previous version. Instead of feeding it into the function as the n-th argument, it explicitely feeds it into the argument named bar.

foo('bar=1')
# bar=1 (type: <class 'str'>)

This is a positional argument, with the string 'bar=1'. Python doesn't care what the content of that string is, it is and stays the string with the content 'bar=1'.

I suspect what you are trying to do is to feed the function named arguments, but with the arguments defined in a dict-like structure. You can achieve that like this:

args = {'bar': 1}
foo(**args)
# 1 (type: <class 'int'>)

This way, the dict args gets taken as named arguments of that function.

Now back to the question you originally had, which is, how to use the string 'bar=1' to achieve the same result. I don't think this is easily possible. It would involve parsing the string manually, then setting up that dict of named arguments. I'm not sure what you are trying to do with it, but there must surely be a better solution than using strings for that.

2 of 6
0

What you're trying to achieve is possible in maybe bash and some other stringly typed languages, not in Python.

This is perhaps the closest you can do in Python.

var_name = 'bar'
var_value = 1

foo(**{var_name: var_value})
# equivalent to foo(bar=1)
Top answer
1 of 3
2

If your code needs to be written just as it is (including the rather odd way of stringing together the ZPL code, and calling a separate script via a shell intermediary, and the avoidance of subprocess, for that matter), you can resolve your issue with a few small adjustments:

First, wrap your code string in double-quotes.

label= '"^XA'+"^FO20,20^BQ,2,3^FDQA,"+"001D4B02107A;1001000;49681207"+"^FS"+"^FO50,50"+"^ADN,36,20"+"^FD"+"MAC: "+"001D4B02107A"+"^FS"+"^FO50,150"+"^ADN,36,20"+"^FD"+"SN: "+"1001000"+"^FS"+"^FO50,250"+"^ADN,36,20"+"^FD" + "Code: "+"49681207"+"^FS"+'^XZ"'

Second, make sure you're actually calling python from the shell:

command = "python script2.py "+label

Finally, if you're concerned about special characters not being read in correctly from the command line, use unicode_escape from codecs.decode to ensure correct transmission.
See this answer for more on unicode_escape.

# contents of second script
if __name__ == "__main__":
    from codecs import decode
    import sys
    zplString = decode(sys.argv[1], 'unicode_escape')
    print(zplString)

Now the call from your first script will transmit the code correctly:

import sys
import os

sys.stdout.flush()
exitCode = os.system(str(command))

Output:

^XA^FO20,20^BQ,2,3^FDQA,001D4B02107A;1001000;49681207^FS^FO50,50^ADN,36,20^FDMAC: 001D4B02107A^FS^FO50,150^ADN,36,20^FDSN: 1001000^FS^FO50,250^ADN,36,20^FDCode: 49681207^FS^XZ
2 of 3
0

Some demo code:

import sys

if __name__ == "__main__":
    for i, arg in enumerate(sys.argv):
        print("{}: '{}'".format(i, arg))

when called like

python test.py ^this^is^a^test

it gives

0: 'test.py'
1: 'thisisatest'

when called like

python test.py "^this^is^a^test"

it gives

0: 'test.py'
1: '^this^is^a^test'

Solution: enclose your parameter string in double-quotes, ie

label = '"' + label + '"'
🌐
Python Forum
python-forum.io › thread-30770.html
passing a string to a function to be a f-string
i don't know how easy or hard this would be to do. i want to have a function that gets a sting from a caller and make it be like an f-string and do it it the context of the caller even though the caller does not have it as an f-string literal. it m...
🌐
GitHub
hplgit.github.io › primer.html › doc › pub › input › ._input-readable003.html
Turning user text into live objects
The eval functions takes a string as argument and evaluates this string as a Python expression. The result of an expression is an object.