How to Fix: SyntaxError: positional argument follows keyword argument

3D illustration of a labeled box blocking an unlabeled box on a conveyor belt, representing the positional argument syntax error.

This SyntaxError positional argument is a simple but strict rule about how you call functions in Python.

The Rule: You can pass arguments in two ways:

  1. Positional: By position ("Alice", 30).
  2. Keyword: By name (job="Engineer").

When you call a function, all positional arguments must come before all keyword arguments.

⚡ Quick Fix: SyntaxError: positional argument follows keyword argument — Python Function Call Argument Order Fix for Positional-First Rule

Python rejected your function call because a bare value appeared after a name=value argument — once you name one argument, every argument after it must also be named.

# WRONG — keyword argument in the middle, positional after it
def create_user(name, age, job):
    print(f"User: {name}, Age: {age}, Job: {job}")

create_user("Alice", age=30, "Engineer")
# SyntaxError: positional argument follows keyword argument

# WRONG — same error fires on any built-in or library function
print("Hello", end="\n", "World")   # SyntaxError: positional argument follows keyword argument

# FIX 1 — all positional: simplest, fastest, order matches function signature
create_user("Alice", 30, "Engineer")

# FIX 2 — all keyword: most readable, order doesn't matter
create_user(name="Alice", age=30, job="Engineer")

# FIX 3 — valid mix: positional arguments FIRST, keyword arguments LAST
create_user("Alice", 30, job="Engineer")   # "Alice" and 30 are positional, job is keyword

The breakdown below shows the exact ordering rule Python enforces and the valid mixed-argument patterns you can use safely.

The Cause

You passed a keyword argument, and then tried to pass a positional argument after it.

Problem Code: Let’s say we have this function:

def create_user(name, age, job):
    print(f"User: {name}, Age: {age}, Job: {job}")

Now we call it, but we mix up the order:

# 1. Positional (OK)
# 2. Keyword (OK)
# 3. Positional (NOT OK!)
create_user("Alice", age=30, "Engineer")
# CRASH! SyntaxError: positional argument follows keyword argument

Python sees "Engineer" and thinks, “I’m already past the positional part. I don’t know where this one goes!”

The Fixes

Fix 1: Make it a Keyword Argument

The easiest fix is to just give the last argument its name.

# All keyword arguments
create_user(name="Alice", age=30, job="Engineer")

Fix 2: Reorder Your Arguments

Move all your positional arguments to the front.

# All positional arguments
create_user("Alice", 30, "Engineer")

# A valid mix
create_user("Alice", 30, job="Engineer")

SyntaxError: positional argument follows keyword argument — The Argument Order Rule and Three Call Patterns

SyntaxError: positional argument follows keyword argument fires before Python runs a single line. The parser validates argument order at parse time — positional after keyword is a structural violation, not a runtime type mismatch.

The rule is simple and absolute: positional arguments first, keyword arguments last, no exceptions.

A positional argument is a bare value: “Alice”, 30, True. Python assigns it by position — first value goes to the first parameter, second value goes to the second parameter.

A keyword argument is a named value: name=”Alice”, age=30. Python assigns it by name regardless of position.

The moment Python sees name=”Alice” (a keyword argument), it closes the positional slot. Any bare value after that — “Engineer” — has no position to land in. Python fires SyntaxError immediately.

Three call patterns are valid. One is not.

All positional: create_user(“Alice”, 30, “Engineer”). Clean, compact, matches the function signature order exactly. Breaks silently if you swap values — “Engineer” in the age slot produces no error, just wrong data.

All keyword: create_user(name=”Alice”, age=30, job=”Engineer”). Most explicit, order-independent, self-documenting. Preferred for functions with many parameters or optional defaults.

Mixed — positional first, keyword last: create_user(“Alice”, 30, job=”Engineer”). Valid — “Alice” and 30 fill name and age by position, job is named explicitly. Use this when the first few arguments are obvious from context and the later ones benefit from being named.

The pattern worth knowing for library calls: pandas, matplotlib, and scikit-learn functions use keyword arguments extensively. pd.read_csv(“data.csv”, sep=”,”, header=0) — positional filename first, keyword options after. Follow this pattern on every library function call and the SyntaxError never appears.

Similar Posts

Leave a Reply