How To Find length of a string in Python?

Published On: 18/06/2022 | Category: Python

Hi Guys,

Today, I would like to show you how to find a length of a string in python. you'll learn python get length of string. we will help you to give example of python get size of string. if you want to see example of python if length of string then you are a right place.

Here i will simple and easy example of how to get length of string in python. So let's see the below example:

Example 1 : Using len()
# Python find string length
# using len

str = "tuts-station.com"
print(len(str))
Output:
16
Example 2 : Using for Loop
# Python find string length
# using for loop

# Returns length of string
def findLength(str):
    counter = 0
    for i in str:
        counter += 1
    return counter
str = "Welcome to Tuts-Station"
print(findLength(str))
Output:
23
Example 3 : Using While Loop
# Python find string length
# using while loop

# Returns length of string
def findLength(str):
    counter = 0
    while str[counter:]:
        counter += 1
    return counter
str = "Hello World"
print(findLength(str))
Output:
11
Example 4 : Using Join and Count
# Python find string length
# using join and count

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

str = "Welcome to Tuts-Station"
print(findLength(str))
Output:
23

I Hope It will help you....