0

I am new to Python, and I kept getting an error that I cannot figure out. I first created a string for the example:

g = "Okay, let's create a line of text here for the example."

Then, I ran the following line and it gave me the correct answer:

g.split()[0].strip(',')
'Okay'

However, when I added in the input argument name, it gave me an error message

g.split()[0].strip(chars=',')

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-284-b2d7ee2c8c2c> in <module>
----> 1 g.split()[0].strip(chars=',')

TypeError: strip() takes no keyword arguments

Am I not allowed to include the input variable name? Python seems to be okay with it when I did that for split():

g.split(sep = ',')
['Okay', " let's create a line of text here for the example."]

Thank you for your help in advance.

magic
  • 1
  • 1
    You are confusing positional arguments with keyword arguments, as explained in [this post](https://stackoverflow.com/questions/9450656/positional-argument-v-s-keyword-argument). Make sure you always check the [docs](https://docs.python.org/3/) for the functions you use to understand how they should be called. – costaparas Jan 30 '21 at 05:47

1 Answers1

0

As per the python documentation (https://docs.python.org/3.4/library/stdtypes.html?highlight=strip) there is no keyword argument for strip. The documentation does specify str.strip([chars]), but in this case chars is a reference to the str (denoted by the brackets around chars). A look at the function above in the documentation you see str.startswith(prefix[, start[, end]]), where prefix is a positional argument. Then as mentioned in the above text, keyword arguments are also possible and generally denoted with the keyword = 'default_value'.

WolVes
  • 1,286
  • 2
  • 19
  • 39
  • Thank you for the explanation. When I ran `g.strip?` where `g` was defined in my original post, Python gave me the following help document: `Signature: g.strip(chars=None, /)`.... It seems that `chars` is a keyword argument? Am I missing something? Sorry, if this looks trivial. I only switched to Python a couple of days ago. – magic Jan 31 '21 at 07:07
  • I figured it out! I didn't understand what the forward slash in the function definition means. Another post mentioned that it "means that all arguments prior to the forward slash are positional only". (https://stackoverflow.com/questions/57235761/why-i-cant-pass-keyword-argument-to-list-index-method). @WolVes, thank you so much for your help! – magic Jan 31 '21 at 07:44