To fix the TypeError: str.replace() takes no keyword arguments in Python, I will show you the cause of this error and some troubleshooting methods, such as passing only the positional argument to the replace function, using the re.sub function, or performing an edit on the list. Read the article below for details.
What causes the TypeError: str.replace() takes no keyword arguments in Python?
Syntax of str.replace():
str.replace(old, new, count)
Parameters:
- old: Original string.
- new: String will replace the original string.
- count: Specify the maximum number of character substitutions.
The replace() function will return a string that is a copy of the original string when the new substring has been replaced with the old substring.
The TypeError: str.replace() takes no keyword arguments often happens when you pass keywords (‘old’, ‘new’) to replace() function.
Example:
testStr = 'visit learnshareit website' # Use the str.replace() function to replace the new string for the old string result = testStr.replace(old = 'website', new = '!') print(result)
Output:
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
result = testStr.replace(old = 'website', new = '!')
TypeError: str.replace() takes no keyword arguments
How to solve this error?
Get positional arguments
The first solution to fix this error is getting positional arguments.
Example:
- Remove the ‘old’, ‘new’ keywords when passing in the function.
testStr = 'visit learnshareit website' # Use the str.replace() function to replace the new string for the old string result = testStr.replace('website','!') print('String after replacing:', result)
Output:
String after replacing: visit learnshareit !
Use the re.sub() function
The re.sub() function also helps you solve this problem.
Syntax:
re.sub(pattern, replace, string, count)
Parameters:
- pattern: is RegEx.
- replace: is the replacement for the resulting string that matches the pattern.
- string: is the string to match.
- count: is the number of replacements. Python will treat this value as 0, match, and replace all qualified strings if left blank.
The Re.sub() method will replace all pattern matches in the string with something else passed in and return the modified string.
Example:
import re testStr = 'visit learnshareit website' # Use the re.sub() function to replace the new string for the old string result = re.sub('website','!', testStr, flags=re.IGNORECASE) print('String after replacing:', result)
Output:
String after replacing: visit learnshareit !
Make edits on the list
Example:
- Convert string to list to make edits.
- Use the del keyword to remove the elements you want.
- Use the append method to add an element to the end of the list.
- Finally, the join() function is used to convert the list to a string.
testStr = 'learnshareit' print('Original string:', testStr) newList = list(testStr) # Use the del keyword to delete the elements you want to remove del newList[10:] # Use the append method to add the element to the end of the list newList.append('!') # Use the join() function to convert a list to string newStr =''.join(newList) print('String after editing:', newStr)
Output:
Original string: learnshareit
String after editing: learnshare!
Summary
The TypeError: str.replace() takes no keyword arguments in Python was quite tricky, but we could fix it. Method 1 is the easiest way to fix. Through my article, you will have your solution. If there is another way, let us know in the comments below. Thanks for reading!
Maybe you are interested:
- TypeError: can’t multiply sequence by non-int of type ‘str’
- TypeError: ‘dict_items’ object is not subscriptable (Python)
- TypeError: ‘datetime.datetime’ object is not callable

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java