-4

I have started learning python programming language and learning string in python.

i am using following code to find the length of string.

a = "Hello, World!"
print(a.len())

but code is not working. I know i am using method len after variable. But why other method works after variable? E.G print(a.lower()) to lower sting, print(a.upper()) to upper string

Alex44
  • 61
  • 1
  • 1
  • 6
  • 2
    `a.len()` doesn't exist. `len(a)` does. – John Coleman Nov 03 '18 at 13:02
  • `len(string)` is mentioned in [the official tutorial](https://docs.python.org/3/tutorial/introduction.html#strings). – Jongware Nov 03 '18 at 13:04
  • 2
    FYI only: "not working" is often a 100% correct description of a problem. However, it is also virtually useless. For any next questions: Python gives quite an *extensive* output error message when you make an error. Make sure to include this in your post. – Jongware Nov 03 '18 at 13:07
  • As to "why": duplicate of https://stackoverflow.com/q/237128/2564301 – Jongware Nov 03 '18 at 13:09

3 Answers3

0

You are doing it wrong. a.len() doesn't exist. You should do

a = "Hello, World!"
print(len(a))

Read more about it here.

0xInfection
  • 2,676
  • 1
  • 19
  • 34
0

dot notations such us .upper(), .lower() are methods and len(), abs() etc are functions Use len(a) instead

PyOrion
  • 86
  • 3
0

It should work across all the Version starting 2.6, reason its not working as mentioned you are trying it wrong as a.len() !

Python 2.6

>>> a = "Hello, World!"
>>> print len(a)
13

Python 3

>>> a = "Hello, World!"
>>> print(len(a))
13

Python 2.7

>>> a = "Hello, World!"
>>> print(len(a))
13
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53