
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Calculate the Length of a String Without Using a Library Function
When it is required to calculate the length of a string without using library methods, a counter is used to increment every time an element of the string is encountered.
Below is the demonstration of the same −
Example
my_string = "Hi Will" print("The string is :") print(my_string) my_counter=0 for i in my_string: my_counter=my_counter+1 print("The length of the string is ") print(my_counter)
Output
The string is : Hi Will The length of the string is 7
Explanation
A string is defined, and is displayed on the console.
A counter is initialized to 0.
The string is iterated over, and after every element is iterated over, the counter is incremented by 1.
This is displayed as output on the console.
- Related Articles
- Write a C program to Reverse a string without using a library function
- Program to find length of a list without using built-in length() function in Python
- Converting number of corresponding string without using library function in JavaScript
- Python Program to Reverse a String without using Recursion
- Golang Program to merge two integer arrays without using library function
- Golang Program to insert an item into the array without using library function
- C++ Program to delete an item from the array without using the library function
- Golang Program to delete an item from the array without using the library function
- C program to copy string without using strcpy() function
- Haskell Program to find the power of a number using library function
- Python Program to Find the Length of the Linked List without using Recursion
- How to calculate the length of the string using C#?
- Write a C program using time.h library function
- Java string length without using length() method.
- Write a C program to convert uppercase to lowercase letters without using string convert function

Advertisements