TypeError: X takes no keyword arguments in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. TypeError: X takes no keyword arguments in Python
  2. TypeError: str.replace() takes no keyword arguments in Python
  3. TypeError: dict.get() takes no keyword arguments in Python

# TypeError: X takes no keyword arguments in Python

The Python "TypeError: X takes no keyword arguments" occurs when we pass a keyword argument to a function that only takes positional arguments.

To solve the error, pass positional arguments to the function.

main.py
def get_name(first, last): return first + ' ' + last # ⛔️ Calling the function with keyword arguments print(get_name(first='bobby', last='hadz')) # ✅ Calling the function with positional arguments print(get_name('bobby', 'hadz'))

The first call to the get_name function uses keyword arguments and the second call uses positional arguments.

Keyword arguments take the form of argument_name=value and positional arguments are passed directly by value.

THe get_name function in the example can be called both ways without any issues.

However, many built-in Python methods don't have names for their arguments, so when you try to call them with a keyword argument, the error is raised.

Instead, you should pass positional arguments to these methods.

Here are 2 examples of how the error occurs with built-in methods and how to solve it.

# Table of Contents

  1. TypeError: str.replace() takes no keyword arguments in Python
  2. TypeError: dict.get() takes no keyword arguments in Python

# TypeError: str.replace() takes no keyword arguments in Python

The "TypeError: str.replace() takes no keyword arguments" occurs when we pass keyword arguments to the str.replace() method.

To solve the error, pass only positional arguments to replace(), e.g. my_str.replace('old', 'new').

python str replace takes no keyword arguments

Here is an example of how the error occurs.

main.py
my_str = 'apple, apple, banana' # ⛔️ TypeError: str.replace() takes no keyword arguments result = my_str.replace(old='apple', new='kiwi')

The error was caused because we passed keyword arguments to the replace() method.

The replace() method takes only positional arguments.

main.py
my_str = 'apple, apple, banana' result = my_str.replace('apple', 'kiwi') print(result) # 👉️ kiwi, kiwi, banana

only passing positional arguments to str replace

The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

Note that the method doesn't change the original string. Strings are immutable in Python.

If you need to use a regular expression when replacing characters in a string, use the re.sub method.

main.py
import re my_str = '1apple, 2apple, 3banana' result = re.sub(r'[0-9]', '_', my_str) print(result) # 👉️ _apple, _apple, _banana

using re sub method with regular expression

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

If the pattern isn't found, the string is returned as is.

# TypeError: dict.get() takes no keyword arguments in Python

The "TypeError: dict.get() takes no keyword arguments" occurs when we pass keyword arguments to the dict.get() method.

To solve the error, only pass positional arguments to dict.get(), e.g. my_dict.get('my_key', 'default').

typeerror dict get takes no keyword arguments

Here is an example of how the error occurs.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} # ⛔️ TypeError: dict.get() takes no keyword arguments result = my_dict.get('name', default='')

The error was caused because we passed a keyword argument to the dict.get() method.

The dict.get() method takes only positional arguments.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} print(my_dict.get('name', '')) # 👉️ "Alice" print(my_dict.get('another', 'default')) # 👉️ 'default'

only pass positional arguments to dict get

The dict.get() method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)
If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

Both of the arguments the dict.get() method takes are positional.

You can't pass a keyword argument to the method.

If the provided key exists in the dict object, its value is returned, otherwise, the get() method returns the supplied default value or None if one wasn't provided when calling dict.get().

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.