# 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

## AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.