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

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

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap