You are currently viewing Python String length with Examples

In this article, we will discuss several ways to return the length of the given string in Python. A string is a set of characters similar to C, C++, and other programming languages.

Advertisements

1. Quick Examples of String length


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("String: ",string1)

# Length of string using len() function
print("String Length using len(): ",len(string1))

# Length of string using for loop
it = 0   
for i in string1:
    it = it + 1
print("String Length using for loop: ",it)

# Length of string using while loop
it = 0   
while (string1[it:]):
        it = it + 1 
print("String Length using while loop: ",it)

# Length of string using sum()
print("String Length using sum(): ",sum(1 for i in string1))

2. String length using len() function

len() function is used to return the total number of characters including spaces present in the given string.

2.1 Syntax of len() function

The following is the syntax of len() function.


# Here string1 is the input string.
len(string1)

2.2 Parameters of len()

len() function takes only one parameter i.e string name.

2.3 Return of len()

len() will return the total number of characters present in the string which is an integer value.

2.4 String len() Example

In this example, we will create a string and return the length of this string.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("String: ",string1)

# Length of string
print("String Length: ",len(string1))

# Output:
# String:  sparkby examples - Python tutorials
# String Length:  35

We can see that there are total 35 characters present in the string including spaces.

3. String length using for loop

In this scenario, we will use a for loop to iterate each character in a string and store it in a variable. Inside for loop, we will increment this variable. Finally we will display this variable that hold the total number of characters present in the string.

3.1 Syntax Structure



# Here i is the iterator
# it variable stores the total number of characters.

it = 0   
for i in string1:
    it = it + 1

3.2 String length with for loop example

In this example, we will create a string and return the length of this string using for loop.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("String: ",string1)

it = 0   
for i in string1:
    it = it + 1

# Length of string
print("String Length: ",it)

# Output:
# String:  sparkby examples - Python tutorials
# String Length:  35

We can see that there are total 35 characters present in the string including spaces.

We can also implement using user-defined function.


# Consider the string
string1 = "sparkby examples - Python tutorials"
print("String: ",string1)

def length_fun(string1):
  it = 0   
  for i in string1:
    it = it + 1
  return it

# Length of string
print("String Length: ",length_fun(string1))

# Output:
# String:  sparkby examples - Python tutorials
# String Length:  35

4. String length using while loop

In this scenario, we will use a while loop to iterate each character in a string and store it in a variable. In while loop, we will use slice operator to iterate the string from first character to last. Inside the while loop we will increment the variable that stores the character count.

4.1 Syntax Structure



it = 0   
while (string1[it:]):
        it = it + 1

4.2 String length with while loop example

In this example, we will create a string and return the length of this string using while loop.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("String: ",string1)

it = 0   
while (string1[it:]):
        it = it + 1

# Length of string
print("String Length: ",it)

# Output:
# String:  sparkby examples - Python tutorials
# String Length:  35

We can see that there are total 35 characters present in the string including spaces.

We can also implement using user-defined function.


# Consider the string
string1 = "sparkby examples - Python tutorials"
print("String: ",string1)

def length_fun(string1):
  it = 0   
  while (string1[it:]):
        it = it + 1
  return it

# Length of string
print("String Length: ",length_fun(string1))

# Output:
# String:  sparkby examples - Python tutorials
# String Length:  35

5. String length using sum()

In this scenario, we will use for loop inside sum() function and we have to pass a Boolean value-True or 1 before it. With this, total characters will be returned.

5.1 Syntax Structure


sum(True for i in string1)
            Or
sum(1 for i in string1)

5.2 String length with sum() example

In this example, we will create a string and return the length of this string using sum() function.


# Consider the string
string1 = 'sparkby examples - Python tutorials'
print("String: ",string1)

# Length of string
print("String Length: ",sum(True for i in string1))

# Output:
# String:  sparkby examples - Python tutorials
# String Length:  35

# Length of string
print("String Length: ",sum(1 for i in string1))

# Output:
# String Length:  35

We can see that there are total 35 characters present in the string including spaces.

6. Conclusion

So in this article, we seen how to return the length of the string using len() function, for loop, while loop and with sum() function. It can be possible to implement the above scenarios using user-defined functions. I have shown this in for loop and while loop scenarios.

Happy learning!!