Find length of a string in python
In this tutorial, we will learn to find the length of a given string. Strings in Python are a sequence of characters wrapped inside single, double, or triple quotes. Strings are immutable, meaning the value of a string cannot be changed. The length of a string is the total number of characters including space and special symbols present in the string. For example,
Input: "Python Language"
Output: 15
To solve this problem there are several different approaches, some of them are:
- Using len() function
- Using loop and a counter variable
- Using loop and slicing
- Using join() and count() function
Approach 1: Using len() function
The len()
is a built-in function of the string class which returns the total number of characters present in a given string.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a string with values
Step 2- Call len() function and pass string
Step 3- Print the result.
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
# length of string
# using len()
s= "Welcome to StudyTonight"
print("Size of string: ",len(s))
Size of string: 23
Approach 2: using loop and counter variable
In this approach, we will iterate over the entire string using a loop and will count the characters with the help of a counter variable. The value of the counter variable will be the length of the string.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will calculate the length of the string
Step 2- Declare and initialize a counter variable
Step 3- Run a loop and iterate over the string
Step 4- Increase the counter variable for every iteration
Step 5- Return counter
Step 6- Call function and print the value
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach. We will define a function that will use the for loop to iterate over the string and count the character.
def findLen(s):
c= 0
for i in s:
c=c+1
return c
s= "Welcome to StudyTonight"
print("Size of string: ",findLen(s))
Size of string: 23
Approach 3: using loop and slicing
In this approach, we will use a while loop and slicing concept. We will slice a string making it shorter by 1 at each iteration till we get an empty string. Break the while loop. Keep a count of the number of iterations this will be the length of the string.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will calculate the length of the string
Step 2- Declare and initialize a counter variable
Step 3- Run a loop and slice the string
Step 4- Increase the counter variable for every iteration
Step 5- Return counter
Step 6- Call function and print the value
Python Program 3
Look at the program to understand the implementation of the above-mentioned approach.
def findLen(s):
c= 0
while s[c:]:
c=c+1
return c
s= "This is a programing tutorial"
print("Size of string: ",findLen(s))
Size of string: 29
Approach 4: using join() and count()
In this approach, we will use a combination of join() and count() methods to calculate the length of a string. The join() method will return the concatenation of two strings. The count() method returns the total characters in a string.
We will be using the join() method to concatenate the given string and a new empty string and then count the characters of the new string. Since the count() method will consider the empty string as space and give an extra count of 1, we will subtract 1 from the final count.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will calculate the length of the string
Step 2- Declare and initialize a string
Step 3- Join string in the empty string and count characters
Step 4- Return final count
Step 5- Call function and print the value returned
Python Program 4
Look at the program to understand the implementation of the above-mentioned approach.
# length of string
# using join and count
# function
def findLen(s):
if not s:
return 0
else:
st= ''
return ((st).join(s)).count(st)-1
s= "This"
print("Size of string: ",findLen(s))
Conclusion
In this approach, we have discussed the four approaches for finding the length of the string. Using built-in methods of the String class like len(), join() and count().