Python Find Length of String

To find length of string in python program; In this tutorial, you will learn how to find string length with and without using len() function in python.

If you want to know more about python string, you can check out this https://www.tutsmake.com/python-strings-example-tutorial/.

In addition, you will also learn how to write a program to find length of string in python

How to Find Length of String in Python

There are 4 ways to find length of string in python with and without using len() function; as shown below:

  • 1: Find string length in python using len() method
  • 2: Find string length in python using while loop and slicing
  • 3: Get/Find string length in python using For loop and operator
  • 4: Find string length in python using string methods join and count

1: Find string length in python using len() method

In this first method, use Python’s built-in length method to find the length of Python string.

To write a program to find the length of the string in python:

# python program to find string length
# using len

str = "hello"
print(len(str)) 

Output

5

2: Find string length in python using while loop and slicing

In this second method, use while loop and slicing to find the length of Python string.

Next, we will write a program to find the length of the string in python by using python while loop and slicling:

# program to find length of string in python
# using while loop.

# Returns length of string
def getLen(str):
    counter = 0
    while str[counter:]:
        counter += 1
    return counter

str = "Hello"
print(getLen(str)) 

Output

5

3: Get/Find string length in python using For loop and operator

In this third method, use python for loop and in operator to find the length of Python string.

Next, write a program to find the length of the string in python by using python for loop and in operator:

# program to find length of string in python
# using for loop

# Returns length of string
def getLen(str):
    counter = 0
    for i in str:
        counter += 1
    return counter


str = "Hello"
print(getLen(str)) 

Output

5

4: Find string length in python using string methods join and count

In this third method, use python string methods join and count to find the length of the Python string.

Next, write a program to find the length of the string in python by Using string methods join and count:

# program to find length of string in python
# using join and count

# Returns length of string
def getLen(str):
    if not str:
        return 0
    else:
        some_random_str = 'py'
        return ((some_random_str).join(str)).count(some_random_str) + 1

str = "hello"
print(getLen(str)) 

Output

5

Recommended Python Tutorials

  1. Python Program to Convert Uppercase to Lowercase
  2. Convert String Lowercase to Uppercase in Python
  3. Python First Character of String Uppercase
  4. Python Concatenate String and Variable (int, float, etc)
  5. Python: String Join Function
  6. Python String Append Examples
  7. How to replace a character in a string in python
  8. Python – Count Number of Occurrences in String
  9. Python Remove Last Character from String
  10. Python Remove Character From String
  11. Python Program to Reverse String
  12. Python Program to Remove First Occurrence of Character in a String
  13. Python: Remove Special Characters from String
  14. Python Program Count vowels in String
  15. Python Split a String into Array of Characters, Space
  16. Python Program to Swap Two Character of Given String
  17. Python Program String Find
  18. Python: Two Given Strings and Swap First Two Characters of Each String
  19. Print All Permutations of Given String in Python
  20. Python | Check Whether a String Contains Number or Letter
  21. Python: Convert String to Float & Float to String
  22. Python Program to Reverse String Using Stack
  23. How To Convert Python Int to String and String to Int
  24. Python Convert String to List and List to String

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Python Find Length of String

  1. Thank you for sharing this insightful and well-written blog post. I enjoyed the way you connected theory with real-life examples, making the content relatable and applicable.

Leave a Reply

Your email address will not be published. Required fields are marked *