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 ProgrammingPython Online CompilerPython Training Tutorials for BeginnersSquare Root in PythonAddition of two numbers in PythonNull Object in PythonPython vs PHPTypeError: 'int' object is not subscriptablepip is not recognizedPython CommentPython Min()Python FactorialPython Continue StatementArmstrong Number in PythonPython lowercasePython UppercasePython map()Python String ReplacePython String findPython Max() FunctionInvalid literal for int() with base 10 in PythonTop Online Python CompilerPolymorphism in PythonInheritance in PythonPython : end parameter in print()Python String ConcatenationPython Pass StatementPython EnumeratePython New 3.6 FeaturesPython input()Python String ContainsPython evalPython zip()Python Range Python IDEInstall Opencv Python PIP WindowsPython String Title() MethodString Index Out of Range PythonPython 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 PythonPython Reverse StringBubble Sort in PythonAttribute Error PythonPython Combine ListsPython slice() functionConvert List to String PythonPython list append and extendPython Sort Dictionary by Key or Valueindentationerror: unindent does not match any outer indentation level in PythonRemove Punctuation PythonCompare Two Lists in PythonPython InfinityPython KeyErrorPython Return Outside FunctionPangram Program in Python
  • Tutorials

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

Tag » What Does Len Do In Python