0

(New to python and stack overflow)

I was curious if there was a way to count the amount of letters in a string for python. for example:

string="hello"

I just want something to count the letters then output it into a variable for later use.

J_man
  • 61
  • 5

2 Answers2

1

The following will give the length of a string:

len(string)

In your case, you can assign it:

numLetters = len(string)

This function can be used for other objects besides strings. For additional uses, read the documentation.

intcreator
  • 4,206
  • 4
  • 21
  • 39
1

Use python function len, i.e.:

size = len(string)

len()

https://docs.python.org/2/library/functions.html#len


DEMO

https://ideone.com/mhpdLi

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268