-4

I'm trying to understand how I should read python documentation, for example given:
string.lstrip(s[, chars])
How should I read that? I know that brackets means optional, but that 's', what does it mean? Is there a page where it explains how the documentation was written?

7
  • s is the function's first parameter. There's nothing significant about the choice of the letter s; it's just a name. Commented Jun 2, 2014 at 20:52
  • 2
    It's not optional; it's mandatory. Note that the method str.lstrip is something else. That method only takes a single, optional argument. Commented Jun 2, 2014 at 20:54
  • 1
    The word "string" here indicates the string module, not a string variable. It is literally "string". Commented Jun 2, 2014 at 20:55
  • 2
    @antox You're looking at the documentation for the string module, not the string type. Commented Jun 2, 2014 at 20:55
  • 1
    Here's the documentation for the method you're using. It's important to read the right documentation. Commented Jun 2, 2014 at 20:55

1 Answer 1

0

It's not explictly defined in the documentation, but in

string.lstrip(s[, chars])

string is a Python module, it is not any string (e.g. it can't be "abc").

The parameter s is the string (e.g. it can be "abc") that you want to strip. It's mandatory, not optional.

The bracket-enclosed parameter is optional, it will be a string and its characters will be stripped from the string.

Some examples of how to call this function are:

import string

print string.lstrip("abc", "a") # "bc"
print string.lstrip(" abc") # "abc"

Note: Don't confuse with "abc".lstrip(). They are different functions with identical results. Also, read @user2357112's comment.

Edit: On my IDE I've just tested it and it actually shows what is s on the docs (pressing F2):

def lstrip Found at: string

def lstrip(s, chars=None):
    """lstrip(s [,chars]) -> string

    Return a copy of the string s with leading whitespace removed.
    If chars is given and not None, remove characters in chars instead.

    """
    return s.lstrip(chars)

# Strip trailing tabs and spaces
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the documentation explicitly says not to use this: "The following list of functions are also defined as methods of string and Unicode objects; see section String Methods for more information on those. You should consider these functions as deprecated, although they will not be removed until Python 3."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.