Forum

Notifications
Clear all

Intermediate Python - Loop Data Structures  

   RSS

0

Can I get some help in understanding this part?

cars["COUNTRY"] = cars["country"].apply(str.upper)

vs 

brics["name_length"] = brics["country"].apply(len)

What is the difference between upper() and len? 

Why need to add "str." to "upper"?

 

3 Answers
1

My understanding is that ...

upper() is a Python string method

len() is a Python built-in function

0

@basil-han is right.

For str.upper, we normally call it with my_string.upper(). It's a string method, ie. every string has access to this method.

For len, we normally call it with len(my_string). It's a built-in function.

0

str.upper simply returns a string where all characters are in upper case.

Example: Let's say you have a string "abc". After you apply the upper() method, your output result will become "ABC".

 

len method simply return the length of a given string.
Example: Let's say you have a string "abc". After you apply the len() method, output result will become 3.

 

Share:

Delete your account