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
- Python Program to Convert Uppercase to Lowercase
- Convert String Lowercase to Uppercase in Python
- Python First Character of String Uppercase
- Python Concatenate String and Variable (int, float, etc)
- Python: String Join Function
- Python String Append Examples
- How to replace a character in a string in python
- Python – Count Number of Occurrences in String
- Python Remove Last Character from String
- Python Remove Character From String
- Python Program to Reverse String
- Python Program to Remove First Occurrence of Character in a String
- Python: Remove Special Characters from String
- Python Program Count vowels in String
- Python Split a String into Array of Characters, Space
- Python Program to Swap Two Character of Given String
- Python Program String Find
- Python: Two Given Strings and Swap First Two Characters of Each String
- Print All Permutations of Given String in Python
- Python | Check Whether a String Contains Number or Letter
- Python: Convert String to Float & Float to String
- Python Program to Reverse String Using Stack
- How To Convert Python Int to String and String to Int
- Python Convert String to List and List to String
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.