BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Calculate length of a String

Last Updated: April 6, 2018 by Chaitanya Singh | Filed Under: Python Examples

In this article we will see Python programs to find the length of a String.

1. Python program to calculate length of a String without using len() function

First we will see how to find the length of string without using library function len(). Here we are taking the input from user and counting the number of characters in the input string using for loop.

# User inputs the string and it gets stored in variable str
str = input("Enter a string: ")

# counter variable to count the character in a string
counter = 0
for s in str:
      counter = counter+1
print("Length of the input string is:", counter)

Output:

Enter a string: Beginnersbook
Length of the input string is: 13

2. Program to find the length of string using library function

In the above program we have not used the library function to find length, however we can do the same thing by using a built-in function. The function len() returns the length of a given string. Lets have a look at the following code:

# User inputs the string and it gets stored in variable str
str = input("Enter a string: ")

# using len() function to find length of str
print("Length of the input string is:", len(str))

Output:

Enter a string: Chaitanya
Length of the input string is: 9

Related Python Examples

  1. Python program to check Alphabet
  2. Python program to check vowel or consonant
  3. Python program to print Hello World
  4. Python program to add two matrices
  5. Python program to find factorial of a number

Top Related Articles:

  1. Python Program to Check if a Number is Positive Negative or Zero
  2. Python Program to Find ASCII Value of a Character
  3. Python Program to Print Calendar
  4. Python Program to Convert Celsius To Fahrenheit and Vice Versa
  5. Python Program to Convert Decimal to Hexadecimal

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap