-4

I recently started learning python. Why can't the lowercase() function accept any input like the str() function?

I learned that str() takes an input and converts the input to a string and outputs the string. Why can't lowercase() take an input and convert the input in to lowercase letters and output the lower cased result?

6
  • .lower() is a method that converts the string object to lowercase. It doesn't accept any arguments Commented Jan 24, 2024 at 22:00
  • str() takes an argument because it can be used with values of almost any type. But lower() is a method of the str class, so it's called as a method: string.lower(). Commented Jan 24, 2024 at 22:06
  • It doesn't make much sense for lowercase() to be a regular function, since most types have no notion of uppercase and lowercase -- it's something specific to strings. Commented Jan 24, 2024 at 22:08
  • This is a similar question but I think its answer is not particularly satisfactory: stackoverflow.com/questions/63531643/… Commented Jan 24, 2024 at 22:08
  • 2
    @journpy: I shudder a bit when newbies are pointed to lambdas, let alone lambdas assigned to names (which have no advantage over def functions outside of code golf). The function the OP seems to want would also convert to str AFAICT, so using proper def functions, and performing the conversion, it would look like def lowercase(obj): return str(obj).lower() Commented Jan 24, 2024 at 22:23

1 Answer 1

0

There is no lowercase() function, so your question is a little confusing from the get-go (str is not a function either, it's a class you construct, but it's very function-like, so I get the confusion there).

Python doesn't provide a top-level lowercase function because it's not a common interface many types need (str, bytes and bytearray are the only built-ins that support case changes to my knowledge), so rather than making it one of the special methods with built-in support, they leave it as a simple named method of the types that need it:

mystring = "mIxEdCaSe"
lowerstring = mystring.lower()  # Produces "mixedcase"

The ability to use str to perform the conversion, then use methods to manipulate the resulting string, avoids dumping hundreds of functions in the built-in namespace that just mean "call str, then this method on it" and makes the functionality more discoverable (once you have a str in the interactive interpreter, you can type mystring.<TAB> and see the available methods, rather than looking at hundreds of built-in functions, most of which would have nothing to do with strings).


As a side-note: There is one built-in that combines class construction and calling a simple named method on it: sorted, which is exactly equivalent to a function like:

def sorted(iterable, /, **kwargs):
    newlist = list(iterable)
    newlist.sort(**kwargs)
    return newlist

It exists mostly because that particular action (converting some other iterable to a list and sorting it) is performed so frequently (in my experience, I call sorted at least 10x as often as I .sort() a list in-place, because I'm usually using a "better" data structure for the task up until I need it sorted) that it was worth the built-in namespace pollution. But it's the exception, not the rule; for other cases, if you can do it with a class constructor followed by a method call, they expect you to do that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.