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)

Python len() function: get string length or number of bytes

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.

  1. Getting String Length with len() in Python:

    • The len() function returns the number of characters in a string.
    # Example
    text = "Python is awesome"
    length = len(text)
    
  2. Counting Bytes with len() in Python:

    • In Python 3, 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"))
    
  3. How to Use len() to Measure String Length in Python:

    • Measure the length of a string using the len() function.
    # Example
    phrase = "Hello, World!"
    length = len(phrase)
    
  4. Using len() for Byte Count in Python:

    • Measure the byte count of a string using len() after encoding.
    # Example
    text = "Python"
    byte_count = len(text.encode("utf-8"))
    
  5. Determining the Length of a String in Python with len():

    • Use len() to determine the length of a string.
    # Example
    sentence = "Programming is fun"
    length = len(sentence)
    
  6. Measuring Byte Length of a String Using len() in Python:

    • Measure the byte length of a string using len() after encoding.
    # Example
    word = "Python"
    byte_length = len(word.encode("utf-8"))
    
  7. Byte Size vs Character Count with len() in Python:

    • Recognize the difference between byte size and character count when using len().
    # Example
    text = "Café"
    character_count = len(text)
    byte_size = len(text.encode("utf-8"))
    
  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)
    
  9. Handling Variable-Length Encoding with len() in Python:

    • Consider variable-length encoding when working with characters that may have different byte lengths.
    # Example
    emoji = "😊"
    byte_length_emoji = len(emoji.encode("utf-8"))