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 😊

Leave a Comment