Python String Length | Len() Method With Example - STechies

Online Tutorials & Training Materials | STechies.com Register Login Python Photoshop SAP Java PHP Android C++ Hadoop Oracle Interview Questions Articles Other Online Tutorials & Training Materials | STechies.com HomeLearn Python ProgrammingAddition of two numbers in PythonPython Online CompilerSquare Root in PythonPython Training Tutorials for BeginnersPython Min()Python map()Python FactorialPython Commentpip is not recognizedTypeError: 'int' object is not subscriptablePython vs PHPNull Object in PythonPython Max() FunctionPython String ReplacePython Continue StatementPython String findPython lowercaseArmstrong Number in PythonPython UppercasePython : end parameter in print()Python New 3.6 FeaturesInheritance in PythonPolymorphism in PythonPython String ConcatenationPython EnumerateTop Online Python CompilerPython RangePython input()Invalid literal for int() with base 10 in PythonPython String ContainsPython evalPython Pass StatementPython zip()String Index Out of Range Python Python IDEPython String Title() MethodInstall Opencv Python PIP WindowsPython Reverse StringPython Print Without NewlineId() function in PythonPython Split()Reverse Words in a String PythonOrd Function in PythonOnly Size-1 Arrays Can be Converted to Python ScalarsArea of Circle in PythonBubble Sort in PythonAttribute Error PythonPython Combine ListsPython InfinityPython KeyErrorPython Return Outside FunctionConvert List to String PythonCompare Two Lists in PythonPython Sort Dictionary by Key or Valueindentationerror: unindent does not match any outer indentation level in PythonRemove Punctuation PythonPython slice() functionPython list append and extendPangram Program in Python
  • Tutorials

  • Learn Python Programming
  • Addition of two numbers in Python
  • Python Online Compiler
  • Square Root in Python
  • Python Training Tutorials for Beginners
  • Python Min()
  • Python map()
  • Python Factorial
  • Python Comment
  • pip is not recognized
  • TypeError: 'int' object is not subscriptable
  • Python vs PHP
  • Null Object in Python
  • Python Max() Function
  • Python String Replace
  • Python Continue Statement
  • Python String find
  • Python lowercase
  • Armstrong Number in Python
  • Python Uppercase
  • Python : end parameter in print()
  • Python New 3.6 Features
  • Inheritance in Python
  • Polymorphism in Python
  • Python String Concatenation
  • Python Enumerate
  • Top Online Python Compiler
  • Python Range
  • Python input()
  • Invalid literal for int() with base 10 in Python
  • Python String Contains
  • Python eval
  • Python Pass Statement
  • Python zip()
  • String Index Out of Range Python
  • Python IDE
  • Python String Title() Method
  • Install Opencv Python PIP Windows
  • Python Reverse String
  • Python Print Without Newline
  • Id() function in Python
  • Python Split()
  • Reverse Words in a String Python
  • Ord Function in Python
  • Only Size-1 Arrays Can be Converted to Python Scalars
  • Area of Circle in Python
  • Bubble Sort in Python
  • Attribute Error Python
  • Python Combine Lists
  • Python Infinity
  • Python KeyError
  • Python Return Outside Function
  • Convert List to String Python
  • Compare Two Lists in Python
  • Python Sort Dictionary by Key or Value
  • indentationerror: unindent does not match any outer indentation level in Python
  • Remove Punctuation Python
  • Python slice() function
  • Python list append and extend
  • Pangram Program in Python
    Python » Python Tutorial
Python String Length | len() Method with Example

Updated Jan 08, 2020

What is len() function in Python

Python len() function is used to calculate the length of the list in Python. It is an inbuilt function in python that returns the length (number of characters) of string in Python.

Parameter of len() Function

len() function takes string or list of elements as a parameter

Syntax of len() Function

len(string)

Return Value of len() Function Python

The output prints the length of the string or number of elements present in the list.

Note: If an invalid argument is passed then a TypeError Exception will be raised

An example of the same would be-

Code to find the length of a string in Python

Please follow the example below to get the length of the string using len().

stringlenth = 'Stechies' print("String Length: ",len(stringlenth))

Output

String Length: 8

Find the number of elements in a list in Python

#!/usr/bin/python listB, listD = [9977, 'xyz', 'sahara', 'stechies'], [459, 'pqr', 234] print("Length of list B : ", len(listB)) print("Length of list D : ", len(listD))

Output

Length of list B : 4 Length of list D : 3

Calculate the length of a string without using len() function in Python

However, if you are interested in finding the length of a string without using len() function, you can follow this example instead.

def string_length(s): if s == '': return 0 return 1 + string_length(s[1:]) len = string_length("Enter String :") print("The length is :", len)

Output

('The length is :', 14)

Recommended Posts:
  • Addition of two numbers in Python
  • Python map()
  • pip is not recognized
  • Null Object in Python
  • Python String find
  • Python Uppercase
  • Polymorphism in Python
  • Python Enumerate
  • Python eval
  • Python Pass Statement
  • Python IDE
  • Python Reverse String
  • Reverse Words in a String Python
  • Ord Function in Python
  • Python Infinity
  • Python Return Outside Function
  • Python Sort Dictionary by Key or Value
  • Remove Punctuation Python
  • Python slice() function
  • Pangram Program in Python
×

Tag » What Does Len Do In Python