ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Shivang Yadav, on April 15, 2021

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

Python String Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.