Python program to find the length of a string (different ways)

Here, we will get user input for string and then print the length of the string using different ways in Python.
By Shivang Yadav Last updated : March 01, 2024

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Strings in Python are immutable means they cannot be changed once defined.

Length of string is the total number of characters present in the string.

Find the length of a string

We will take a string as input from the user and then find and print the length of the string.

Python provides multiple ways to find the length of the string.

Example:

Input : "includehelp" 
Output: 11

Method 1: Using the len() method

Python provides multiple methods to find the length of the given string. There is a built-in method len() to find the length of the string.

Syntax

len(string_name)

Program to find the length of string using len() method

# Python program to find the length of
# the string using len() method 

# Getting string input from the user 
myStr =  input('Enter the string : ')

# Finding the length of the string 
strLen = len(myStr)

# printing values 
print("Entered String is ", myStr)
print("Length of the string : ", strLen)

Output:

RUN 1:
Enter the string : Hello world.
Entered String is  Hello world.
Length of the string :  12

RUN 2:
Enter the string : Hello, Reader! How are you?
Entered String is  Hello, Reader! How are you?
Length of the string :  27

Method 2: Using the loop

To find the length of a string, we need to loop over the string and then count the characters. Then print the size.

Algorithm

  • Initialise : size = 0
  • Take string as input from the user.
  • Loop for all characters in the string,
    • increment the size -> size++
  • Print size.

Program to find the length of string using loop

# Python program to find the length 
# of the string using loop 

# Getting string input from the user 
myStr =  input('Enter the string : ')

# Finding the length of the string 
strLen = 0   
for i in myStr:
    strLen = strLen + 1

# printing values 
print("Entered String is ", myStr)
print("Length of the string : ", strLen)

Output:

Enter the string : IncludeHelp.com
Entered String is  IncludeHelp.com
Length of the string :  15

Method 3: Using the string slice method

One more way to find the length of the string is by using a loop with the slice method. We will slice the string by incrementing the counter till the slice makes the string empty.

Program to find the length of string using slice and while

# Python program to find the length of the string

# Getting string input from the user 
myStr =  input('Enter the string : ')

# Finding the length of the string 
strLen = 0   
while myStr[strLen:]:
        strLen = strLen + 1

# printing values 
print("Entered String is ", myStr)
print("Length of the string : ", strLen)

Output:

Enter the string : IncludeHelp
Entered String is  IncludeHelp
Length of the string :  11

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.