Syntaxerror: positional argument follows keyword argument

How to fix the syntaxerror: positional argument follows keyword argument error?

In this article, we will answer your question, but first, let’s understand this error and why it happens.

It is because if we have a better understanding of this error technically, we can resolve the smoothly and effectively.

What is “syntaxerror positional argument follows keyword argument”?

The syntaxerror: positional argument follows keyword argument occurs when you are trying to use a positional argument in a function after you use a keyword argument. 

In Python, positional arguments are interpreted in the order in which they appear first and then followed by the keyword arguments.

In simple words, the error occurs when a positional argument is placed after a keyword argument in a function call in Python.

For example:

def sample (name, message):
  return f'Hi {name}, {message}!'


print(sample(name='Itsourcecoder', 'Welcome to Itsourcecode, which offers free sourcecode and tutorials'))

As you can see in our example code, the function greet takes two arguments, name, and message.

Unfortunately, when calling the function, the argument “Itsourcecode” is placed after the keyword argument message =Welcome to Itsourcecode, which offers free sourcecode and tutorials.

The error was raised because it violates the syntax rules in Python and leads to the following.

Output:

C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py 
  File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 5
    print(sample(name='Itsourcecoder', 'Welcome to Itsourcecode, which offers free sourcecode and tutorials'))
                                                                                                         ^
SyntaxError: positional argument follows keyword argument

What is the distinction between Positional Arguments and Keyword Arguments?

Now let’s understand the distinction between the two arguments:

Positional arguments don’t have “keyword” in front of it.

For example:

 sample_function("Caren", 18)

Positional arguments are arguments that are passed to a function based on their position or order.

Meanwhile,


✔ Keyword arguments have a “keyword” in front of it.

For example:

 sample_function(age=18, name="Caren")

Keyword arguments are arguments that are passed to a function using their associated parameter names.

That is why when you use a positional argument after a keyword argument, you’ll face this error.

For example:

sample_function(age=18, Caren)

Here is an example wherein you can pass the arguments as positional or a keyword argument or even use both. Let’s see the example below:

✅ positional arguments
sample_function('Caren', 'Hi!')

✅keyword arguments
sample_function(greetings='Hi!', name='Caren')

✅1 positional argument and 1 keyword argument
sample_function('Caren', greetings='Hi!')

Why does “positional argument follows keyword argument” error occur?

This error arises because Python follows a specific syntax for function calls, where positional arguments should precede keyword arguments.

These are the following reasons why this error was triggered:

❌ Incorrect placement of positional and keyword arguments in the function call.

❌ Missing or extra commas in the function call.

❌ Mismatch between the function signature and the arguments provided in the function call can also result in this error.

How to fix “syntaxerror: positional argument follows keyword argument”

To fix the error, ensure that all positional arguments are placed before any keyword arguments in a function call.

Solution 1: Change the position of the arguments

Rearrange the order of the arguments in the function call so that the positional arguments are placed before the keyword arguments.

For example:

def sample (name, message):
  return f'Hi {name}, {message}!'

print(sample('Itsourcecoder', message='Welcome to Itsourcecode, which offers free sourcecode and tutorials'))

Output:

Hi Itsourcecoder, Welcome to Itsourcecode, which offers free sourcecode and tutorials!

Solution 2: Provide arguments as keyword arguments

Use the keyword arguments for all the arguments in the function call to explicitly specify the parameter names.

For example:

def sample(name, age):
    print(f"Hi {name}, Are you {age} years old?")

sample(name="Caren", age=18)

Output:

Hi Caren, Are you 18 years old?

Solution 3: Convert the ositional argument to a keyword argument

Assign the value to the argument using its parameter name in the function call, making it a keyword argument.

For example:

def sample(name, age):
    print(f"Hello {name}, Are you {age} years old?")

sample(age=18, name="Caren")

Output:

Hello Caren, Are you 18 years old?

Conclusion

In conclusion, the error message syntaxerror: positional argument follows keyword argument occurs when you are trying to use a positional argument in a function after you use a keyword argument. 

We already discussed above what this error is all about and multiple ways to resolve this error.

By executing the solutions above you can master this Syntaxerror in Python with the help this guide.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

What is Python SyntaxError and what causes it?

SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.

How do I fix ‘unexpected EOF while parsing’?

Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.

Why does my syntax error point to a line that looks correct?

The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.

Why does ‘print x’ raise SyntaxError in Python 3?

Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.

Where can I find more SyntaxError fixes?

Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.

Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment