Python Function Argument as a String: How It Works

Formulas & functions · Updated November 22, 2026

A Python function argument is a string when the value passed to the function is a Python str object, such as 'Ada' or 'hello'. To indicate that a function expects text, give its parameter a name and, optionally, a : str type annotation; the annotation documents the expectation but does not automatically enforce it at runtime.

What a string argument means

In Python, a function can receive values through its parameters. When a call supplies text for one of those parameters, that supplied value is a string argument. For example, in greet('Mina'), the text 'Mina' is an argument, and the function receives it through the parameter named name.

def greet(name):
    return f'Hello, {name}!'

message = greet('Mina')
print(message)  # Hello, Mina!

Here, name is the parameter in the function definition, and 'Mina' is the argument in the function call. People sometimes use “argument” and “parameter” interchangeably in casual discussion, but the distinction is useful: parameters are the names in the definition; arguments are the values supplied when calling the function.

A string can contain a single character, a sentence, digits represented as text, or even an empty value. For example, '7' is a string, while 7 is an integer. The quotes in Python source code identify a string literal; they are not part of the string’s contents.

Writing a function that expects text

A function can accept a string whether or not its definition includes a type annotation. Adding : str makes the intended input clearer to readers and lets compatible editors and static type checkers flag some mismatched calls.

def make_greeting(name: str) -> str:
    return f'Hello, {name}!'

print(make_greeting('Ravi'))

In this example, name: str annotates the parameter and -> str annotates the return value. Neither annotation changes how Python executes the function or forces the caller to provide a string. If someone calls make_greeting(42), ordinary Python execution still allows the call; the result will be 'Hello, 42!' because the f-string converts the integer to text for display. In other functions, a wrong type might instead cause an error when an operation is attempted.

Python type annotations are metadata that tools can use. A type checker may report an integer passed where str is expected, but Python does not reject that call just because of the annotation. A function that must enforce an input rule needs an explicit check, or it needs to rely on a clearly documented contract and handle invalid input appropriately.

How string arguments are passed

Python lets callers supply arguments positionally or by keyword. A positional argument is matched to a parameter by its place in the call. A keyword argument names the parameter directly, which can make a call easier to understand, especially when a function has several inputs.

def describe_item(item: str, category: str) -> str:
    return f'{item} is in {category}.'

first = describe_item('Notebook', 'stationery')
second = describe_item(category='food', item='Pear')

Both calls pass strings to both parameters. In the second call, the order of the keyword arguments does not need to match the order of the parameters. A caller can also combine positional and keyword arguments, but positional arguments must come before keyword arguments in a normal function call.

The function receives references to Python objects. Since strings are immutable, code inside a function cannot change the contents of a string in place. An assignment to the local parameter name only makes that local name refer to a different object; it does not replace the caller’s original variable.

def add_prefix(text: str) -> str:
    text = 'Note: ' + text
    return text

original = 'Bring a pen'
result = add_prefix(original)
print(original)  # Bring a pen
print(result)    # Note: Bring a pen

This is one reason strings are convenient function inputs: the function can build and return a revised string without silently modifying the original string object.

String literals, variables, and input

A string argument can be written directly as a literal or supplied through a variable. String literals can use single quotes or double quotes, and triple quotes can represent strings spanning multiple lines. Choose the form that makes the text readable and makes it easy to include quote characters when needed.

city = 'Oslo'
print(greet(city))
print(greet("Sam's book"))

The second call uses double quotes around the literal so the apostrophe in Sam's does not end the string. A caller should not add quote marks around a variable name to make it a string: greet(city) passes the value stored in city, while greet('city') passes the literal word city.

Text read with the built-in input() function is returned as a string, even when the person types digits. Thus, if a program reads age = input('Age: '), the value in age is text such as '36', not the integer 36. This distinction matters when a function will perform arithmetic rather than display or store the characters.

Defaults, optional values, and annotations

A string parameter can have a default value. The default is used only when the caller omits that argument; it does not prevent a caller from supplying a different value.

def greet(name: str = 'there') -> str:
    return f'Hello, {name}!'

print(greet())       # Hello, there!
print(greet('Lee'))  # Hello, Lee!

If a parameter is required, leave off the default. If a function meaningfully accepts either text or None, express that possibility in its annotation rather than describing the parameter as always a string. In Python 3.10 and later, str | None is a common annotation for this case.

def display_name(name: str | None) -> str:
    if name is None:
        return 'Name not provided'
    return name

An annotation should match the function’s actual intended behavior. It is not the same thing as converting the argument. For example, writing value: str does not turn an integer into text. If conversion is appropriate, do it explicitly with str(value), and consider whether the resulting text has the meaning the program needs.

Converting other values to strings

When a function needs text but a caller has another kind of value, one option is to convert the value before the call. The built-in str() function produces a human-readable string representation for many Python objects.

count = 12
label = 'Items: ' + str(count)
print(label)  # Items: 12

For formatting a mixture of text and values, an f-string is often clearer than manually joining strings. It converts expressions for display inside the formatted result:

def report_count(count: int) -> str:
    return f'There are {count} items.'

Conversion is not validation. Calling str(12) gives '12', but that does not prove that the original value was intended to be a valid name, identifier, or other piece of text. When a function requires a string specifically, it may be better to reject a non-string value than to convert it without the caller’s knowledge.

Checking and validating a string argument

For a runtime check, use isinstance(value, str). It can help a function fail early with a useful message rather than producing a confusing error later. The right check depends on the function’s contract: some functions should accept only strings, while others can reasonably support several types.

def shout(text: str) -> str:
    if not isinstance(text, str):
        raise TypeError('text must be a string')
    return text.upper()

A type check answers whether a value is a string, not whether its contents are acceptable. For instance, a string can be empty or contain only spaces. A function that requires a non-empty name might also check text.strip(), and decide whether to reject, trim, or preserve surrounding whitespace. Validation rules should reflect the actual task rather than imposing assumptions that may be wrong for valid input.

  • Use a type check when the function cannot operate correctly on a non-string value.
  • Check content separately when length, format, or allowed characters matter.
  • Raise a clear exception for invalid data rather than silently changing it in an unexpected way.
  • Document whether the function trims whitespace or preserves the caller’s exact text.

Common mistakes and misconceptions

One frequent mistake is confusing a number written with quotes with a number. '10' is text, so adding it to another string concatenates characters, whereas adding the integer 10 to an integer performs arithmetic. Convert deliberately when crossing between text and numeric data.

Another misconception is that a type hint protects a function from every invalid call. An annotation such as text: str communicates an expectation, but normal Python does not enforce it. Use a runtime check when enforcement is needed, and use a static type checker when you want problems identified during development.

It is also easy to mistake the text a user types for a value of the corresponding type. input() returns text, so numeric input must be parsed, for example with int() or float(), before numeric operations. Parsing can fail for unsuitable text, so robust programs handle the resulting ValueError where appropriate.

Finally, avoid naming a parameter str. That name shadows Python’s built-in str type within the relevant scope and can make later conversions or checks confusing. Names such as text, message, or filename usually say more about the parameter’s purpose.

Frequently Asked Questions

How do I define a Python function argument as a string?
Define a parameter and optionally annotate it with str, as in def show(message: str):. The annotation documents the expected type; add a runtime check if the function must enforce it.

Do I need to put quotes around a string variable when passing it?
No. Pass the variable name without quotes, such as show(message). Quotes around it, as in show('message'), pass the literal word message instead of the variable’s value.

Does name: str convert an argument to a string?
No. It is a type annotation, not a conversion. To convert a value explicitly, call str(value); to require that it already be text, check its type.

Is '123' a string or an integer?
It is a string because it is quoted. The unquoted literal 123 is an integer. Convert text with int('123') when an integer is appropriate, while accounting for text that cannot be parsed.

Can a Python function accept an empty string?
Yes. '' is a valid string, but an application may decide it is not a valid value for a particular parameter. Check for emptiness or whitespace explicitly if the function requires meaningful content.

Choosing the right approach

For most ordinary functions, a descriptive parameter name and a str annotation make the expected input clear. Keep the function’s behavior aligned with that expectation: accept text as text, convert other values only when conversion is intentional, and validate content when the task calls for specific rules. This combination helps callers understand what to pass and helps maintainers see where type, conversion, and content checks belong.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.