
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python String len() Method
The Python String len() method is used to retrieve the length of the string. A string is a collection of characters and the length of a string is the number of characters (Unicode values) in it.
The len() method determines how many characters are there in the string including punctuation, space, and all type of special characters. The number of elements which is stored in the object is never calculated, so this method helps in providing the number of elements.
For example, the string “Tutorials Point” has 15 characters in it including the space.
Syntax
Following is the syntax of Python String len() method:
len(str)
Parameters
This method does not accept any parameter.
Return Value
This method returns the length of the string.
Example
In the following example we are finding the length of the given string "this is string example....wow!!!" using Python String len() method:
str = "this is string example....wow!!!"; print "Length of the string: ", len(str)
Following is the output of the above code:
Length of the string: 32
Example
Following is an example where a list of strings is created. Then using the len() method we return the length of the list:
# finding the length of the list li = ["Python","Java","CSS","Javascript"] # Returning the length of the list print("The length of the given list is:", len(li))
Output of the above code is as follows:
The length of the given list is: 4
Example
In the example given below an array of elements is created. Then by using the len() method, the size of the array is returned:
# finding the length of the array array = ["Jyoti","Radhika","Kriti","Suhana","Akriti","Ankita","Nachiket"] # Returning the length of the list print("The length of the given array is:", len(array))
While executing the above code we get the following output:
The length of the given array is: 7
Example
In the following example a dictionary is created and the length of the dictionary is returned:
# finding the length of the dictionary dictionary = {'Player': 'Sachin Tendulkar', 'Sports':'Cricket', 'age':48} res = len(dictionary) # Returning the length of the list print("The length of the given array is:", res)
When we run above program, it produces following result:
The length of the given array is: 3