Program to Calculate the Length of a String Without Using a Library Function

Python Program to Calculate the Length of a String Without Using a Library Function

Strings in Python:

A string is one of the most frequent data types in any computer language. A string is a collection of characters that can be used to represent usernames, blog posts, tweets, or any other text content in your code. You can make a string and assign it to a variable by doing something like this.

sample_string='BTechGeeks'

Strings are considered immutable in Python, once created, they cannot be modified. You may, however, construct new strings from existing strings using a variety of approaches. This form of programming effort is known as string manipulation.

Examples:

Example1:

Input:

given string = hello this is btechgeeks online

Output:

The length of given string { hello this is btechgeeks online } = 31

Example2:

Input:

given string = for file operations upload files using 23199 -@33hello

Output:

The length of given string { for file operations upload files using 23199 -@33hello } = 54

Program to Calculate the Length of a String Without Using a Library Function in Python

Below is the full approach to calculate the length of the given string without using built-in library len() in Python.

1)Using Count variable(Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take a variable to say stringLength that stores the length of the given string.
  • Initialize the stringLength to 0.
  • To traverse the characters in the string, use a For loop.
  • Increment the stringLength (Count Variable) by 1.
  • Print the stringLength.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
given_string = 'HelloThisisBTechGeeks'
# Take a variable to say stringLength that stores the length of the given string.
# Initialize the stringLength to 0.
stringLength = 0
# To traverse the characters in the string, use a For loop.
for charact in given_string:
    # Increment the stringLength (Count Variable) by 1.
    stringLength = stringLength+1
# Print the stringLength.
print('The length of given string {', given_string, '} =', stringLength)

Output:

The length of given string { HelloThisisBTechGeeks } = 21

Explanation:

  • A string must be entered by the user as static input and saved in a variable.
  • The count variable is set to zero.
  • The for loop is used to go over the characters in a string.
  • When a character is encountered, the count is increased.
  • The total number of characters in the string, which is the string’s length, is printed.

2)Using Count variable(User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take a variable to say stringLength that stores the length of the given string.
  • Initialize the stringLength to 0.
  • To traverse the characters in the string, use a For loop.
  • Increment the stringLength (Count Variable) by 1.
  • Print the stringLength.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using input() function and store it in a variable.
given_string = input('Enter some random string = ')
# Take a variable to say stringLength that stores the length of the given string.
# Initialize the stringLength to 0.
stringLength = 0
# To traverse the characters in the string, use a For loop.
for charact in given_string:
    # Increment the stringLength (Count Variable) by 1.
    stringLength = stringLength+1
# Print the stringLength.
print('The length of given string {', given_string, '} =', stringLength)

Output:

Enter some random string = btechgeeksonlineplatformforgeeks and coding platform
The length of given string { btechgeeksonlineplatformforgeeksa nd coding platform } = 52

The total number of characters in the string, which is the string’s length, is printed.
Related Programs: