How to Get Python String Length?

In Python, different sequence types are used to store multiple input data values such as strings, lists, tuples, etc. The string data type stores a sequence of the alphabet, numbers, or characters. Python supports multiple built-in functions that perform operations on the strings. Some commonly used operations include removing a string, adding a new string, calculating the string’s length, and so on.

This guide will elaborate on how to get string length in Python. The following aspects of string length will be discussed in this Python Guide:

So let’s get started!

How to Get Python String Length?

To determine the length of a Python string, we can use an inbuilt function named “len()” or a user-defined function with “for loops” and “if-else” statements. The syntax of the len() function is shown below:

len(obj)
How to install Kali Linux in Window...

The len() function accepts an object as an argument and retrieves the number of items in that object. The object can be either a sequence or a collection. Passing a string value to the len() function will return the length/number of characters in that string.

The below examples are used to find Python string length:

Example 1: Using the “len()” Function to Get Python String Length

The inbuilt Python function “len()” gets the length of the string. This function returns the number of elements/characters of the passed string. Additionally, this function is used to get the length of a tuple, list, dictionary, and other data storage container.

Code:

value = "Python Guide!"
print(len(value))

In the above code, the string “Python Guide” is initialized in a variable named “value”. The “len()” function takes the variable “value” as an argument and shows the length of that string.

Output:

The output shows the length of a given string value.

Example 2: Getting Length of String Using for Loop

In the example code given below, the “for loop” is used inside the user-defined function to calculate the length of a string value.

Code:

def Length(str_value):
    len = 0
    for i in str_value:
        len += 1
    return len

str_value = "Python String Guide!"
print(Length(str_value))

In the above code:

  • The function named “length” is defined to take the value of the given string inside the parenthesis.
  • The counter value “0” is initialized in the variable “len”. The zero value indicates that the length of the string begins from the first character position.
  • The “for” loop iterates on the string value, and on each character value, it will add “1” in the variable named “len”.
  • The return statement returns the function result to a variable named “len”.

Output:

The output represents the string length named “str_value”.

Example 3: Getting Length of Python String Using if-else Statement

In the following example, the length of the string is calculated using the “if-else” statement inside the user-defined function.

Code:

def Length(str_value):
    if str_value == '':
        return 0
    else:
        return 1 + Length(str_value[1:])
str_value = Length("Python and Linux Guide!")
print(str_value)

In the mentioned code:

  • The function named “length” is defined using the def keyword.
  • The “if-else” statement is initialized inside the “Length” function to find the length of an input string.
  • If the string value is empty, then the “if” statement returns “0”.
  • The “else” statement executes when the string value is not zero. The statement inside the “else” block uses the index method technique to find the length of the string.
  • The statement “[1:]” indicates that the count of string value starts from the 1st character and counts the length until the last character.

Output:

The length is displayed in the output.

That’s all from this Python Guide!

Conclusion

In Python, the “len()” method is used to get the length of the string. The “for loop” and “if-else” statement can be used with the aid of a user-defined function to get the string’s length. The “len()” function is the simplest way to calculate the string’s length. It can also get the length of other data structures such as a dictionary, tuple, list, etc. This write-up guide presented a detailed overview of how to get the length of a string in Python.