How to find the length of a string in Python?

In Python, finding the length of a string is straightforward and can be done using the built-in len() function. This function returns the number of characters in a string, including spaces, punctuation, and special characters. Here’s how you can use it:

Using the len() Function

The len() function is one of Python's built-in functions, and it doesn't only work for strings; it can also determine the length of other iterable data types, such as lists, tuples, dictionaries, and more. Here’s how to use len() to find the length of a string:

# Define a string my_string = "Hello, world!" # Use the len() function to find the length of the string string_length = len(my_string) # Print the length print("The length of the string is:", string_length)

This code will output:

The length of the string is: 13

Example Details

  • my_string is a string variable containing the text "Hello, world!".
  • len(my_string) computes the number of characters in my_string, which includes all letters, punctuation marks, and spaces.
  • The result, 13, is stored in the variable string_length.
  • The print() function is then used to display the length.

Additional Considerations

  • Empty Strings: If the string is empty, len() returns 0.

    empty_string = "" print(len(empty_string)) # Output: 0
  • Unicode Characters: The len() function counts Unicode characters effectively. Each character, regardless of its complexity, counts as one.

    unicode_string = "你好世界" # "Hello world" in Chinese print(len(unicode_string)) # Output: 4
  • Special Characters: Special characters and escape sequences such as newline (\n) or tab (\t) are also counted as a single character.

    special_string = "Line1\nLine2" print(len(special_string)) # Output: 11
  • Strings with Emojis: Emojis or other composite characters are counted based on how they are represented in Python. Usually, each symbol is counted as one character.

    emoji_string = "Hello 👋🌍" print(len(emoji_string)) # Output will depend on the representation but likely is 9

The len() function is a simple and efficient way to determine the length of a string in Python, making it a commonly used tool for string manipulation and processing tasks.

TAGS
System Design Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What are the 5 core values of MS?
How long does Twitter interview process take?
How do I teach myself to be a software engineer?
Refining short-term and long-term strategies for interview success
Is system design asked for freshers?
Explain WebRTC vs WebSocket for Realtime.
Learn the difference between WebRTC and WebSocket for real-time apps. Covers use cases, examples, trade-offs, interview tips, and pitfalls for system design interviews.
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.