1
dog = 'penny'
print(dog.title())

dog_names = ['pete', 'luke', 'shane']
print(dog_names.remove('shane'))

Why does Python return an output of Penny for dog.title() but None for dog_names.remove('shane')? Why can I not print the list dog_name with the method remove while I can use the method title on dog?

I understand that I get None because dog_name.remove has no return, but how does dog.title have a return?

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 3
    https://docs.python.org/3/library/stdtypes.html#str.title – sshashank124 Dec 29 '19 at 05:50
  • [`list.remove()`](https://docs.python.org/3.8/tutorial/datastructures.html#more-on-lists) modifies the list in-place, it is convention for all methods that modify an object in-place to return `None` – Iain Shelvington Dec 29 '19 at 05:59
  • Does this answer your question? [Inplace functions in Python](https://stackoverflow.com/questions/40126403/inplace-functions-in-python) – Georgy Dec 29 '19 at 12:25
  • A better duplicate target: [Why some methods change the object permanently while others don't?](https://stackoverflow.com/q/52469732/7851470) – Georgy Dec 29 '19 at 12:32

3 Answers3

1

The title() function is a pre-defined function in Python which is used to covert the first character of the string into uppercase and the remaining characters into lowercase and return a new string. in your example if you run print(dog) you can see that penny is all lowercase, but if you run print(dog.title()) you can see that the first letter in Penny which is P is uppercase and the remaining is lowercase

0

In order to answer your question, first, you need to understand what's being returned.

When calling print on a function or method, it will display the returned value. With that being said, you can check the return value of both functions from the documentation

https://docs.python.org/2/tutorial/datastructures.html#more-on-lists

list.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item.

https://docs.python.org/3.7/library/stdtypes.html#str.title

str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.

  • Notice how title() returns the titled case string, that's why it's printed.
  • On the other hand remove() doesn't return anything, in this case, python has a default return value of None (Source) that's why it's printed when calling remove()
Marsilinou Zaky
  • 1,038
  • 7
  • 17
0

A function/method in any programming language performs a specific task. A task could be performed on the inputs passed to the function. After performing the task, the function may want to give the result back to the caller of the function. It may do that by:

  1. By modifying the parameters passed to it or By altering the object on which method was called. (For example, remove() method of list data type)
  2. By returning the result to the caller. (title() method)
  3. A combination of (1) and (2)

Your remove() method doesn't return anything back to the caller. Instead it just deletes an element from the list. To print the list after removing an element:

dog_names.remove('shane')
print(dog_names)
abhiarora
  • 9,743
  • 5
  • 32
  • 57