Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll learn about the len()
function in Python, which is a built-in function that returns the number of items (length) in an object. The len()
function can be used with several data types, including strings, lists, tuples, dictionaries, and sets.
Example 1: Using len()
with strings
The len()
function returns the number of characters in a string.
text = "Hello, world!" length = len(text) print(length) # Output: 13
Example 2: Using len()
with lists
The len()
function returns the number of elements in a list.
numbers = [1, 2, 3, 4, 5] length = len(numbers) print(length) # Output: 5
Example 3: Using len()
with tuples
The len()
function returns the number of elements in a tuple.
fruits = ("apple", "banana", "cherry") length = len(fruits) print(length) # Output: 3
Example 4: Using len()
with dictionaries
The len()
function returns the number of key-value pairs in a dictionary.
person = {"name": "John", "age": 25, "city": "New York"} length = len(person) print(length) # Output: 3
Example 5: Using len()
with sets
The len()
function returns the number of elements in a set.
unique_numbers = {1, 2, 3, 4, 4, 5, 5} length = len(unique_numbers) print(length) # Output: 5
In summary, the len()
function is a built-in Python function that returns the number of items (length) in an object. It can be used with various data types, including strings, lists, tuples, dictionaries, and sets. The len()
function is helpful when you need to determine the size or length of a collection or sequence.
Getting String Length with len()
in Python:
len()
function returns the number of characters in a string.# Example text = "Python is awesome" length = len(text)
Counting Bytes with len()
in Python:
len()
also counts the number of Unicode characters. If byte count is needed, consider encoding the string.# Example text = "Python is awesome" byte_count = len(text.encode("utf-8"))
How to Use len()
to Measure String Length in Python:
len()
function.# Example phrase = "Hello, World!" length = len(phrase)
Using len()
for Byte Count in Python:
len()
after encoding.# Example text = "Python" byte_count = len(text.encode("utf-8"))
Determining the Length of a String in Python with len()
:
len()
to determine the length of a string.# Example sentence = "Programming is fun" length = len(sentence)
Measuring Byte Length of a String Using len()
in Python:
len()
after encoding.# Example word = "Python" byte_length = len(word.encode("utf-8"))
Byte Size vs Character Count with len()
in Python:
len()
.# Example text = "Café" character_count = len(text) byte_size = len(text.encode("utf-8"))
Python len()
and Unicode Characters:
len()
counts Unicode characters, so it accurately represents the length of strings containing Unicode characters.# Example unicode_text = "😊 Python" length_unicode = len(unicode_text)
Handling Variable-Length Encoding with len()
in Python:
# Example emoji = "😊" byte_length_emoji = len(emoji.encode("utf-8"))