How to Find the Length of a String in Python

Here are ways to find the length of a string in Python:

  1. Using len()
  2. Using for loop
  3. Using list comprehension
  4. Using while loop with slicing
  5. Using join()
  6. Using reduce()
  7. Using enumerate()

Method 1: Using len()

The built-in len() function returns the number of characters(including spaces and punctuation) in the string.

Visual Representation

Visual Representation of Find the Length of a String in Python Using len()

Example

new_string = 'Leo Messi'

length_of_string = len(new_string)

print("Length of the string:", length_of_string)

Output

Length of the string: 9

Method 2: Using for loop

A for loop iterates over each character in the string, incrementing the count variable by 1 for each character.

Example

new_string = 'Leo Messi'

count = 0
for x in new_string:
 count += 1

print("Length of the string:", count)

Output

Length of the string: 9

Method 3: Using list comprehension

You can use List comprehension with the sum() function to iterate over string and generate a sequence of 1s, one for each character. The sum of this sequence provides the total length of the string.

Visual Representation

Visual Representation of Using list comprehension

Example

new_string = 'Leo Messi'

length_of_string = sum(1 for _ in new_string)

print("Length of the string:", length_of_string)

Output

Length of the string: 9

Method 4: Using while loop with slicing

The while loop iterates through string, removing the first character in each iteration using slicing, while incrementing count. This process continues until the string is empty, with count then representing the length of the string.

Example

new_string = 'Leo Messi'
count = 0

while new_string:
 new_string = new_string[1:] # Remove the first character of the string
 count += 1

print("Length of the string:", count)

Output

Length of the string: 9

Method 5: Using join()

The join() function is used to join a unique separator between each character of string, and then the length of the string is found by counting these separators and adding 1.

Example

new_string = 'Leo Messi'

separator = 'X' # unique separator

# Convert the string into a list of characters and join them with the separator
joined_string = separator.join(list(new_string))

# Count the number of separators in the joined string
# The length of the original string is the count of separators plus 1
length_of_string = joined_string.count(separator) + 1

print("Length of the string:", length_of_string)

Output

Length of the string: 9

Method 6: Using reduce()

The reduce() function, combined with a lambda function, iterates over the string, incrementing a counter for each character encountered.

Visual Representation

Visual Representation of Using reduce()

Example

from functools import reduce

new_string = 'Leo Messi'

length_of_string = reduce(lambda count, _: count + 1, new_string, 0)

print("Length of the string:", length_of_string)

Output

Length of the string: 9

Method 7: Using enumerate()

The enumerate() function is used for iterating over sequences while keeping track of both the index and the value.

Example

new_string = 'Leo Messi'

count = 0

for index, x in enumerate(new_string):
 count += 1 

print("Length of the string:", count)

Output

Length of the string: 9

Related posts

Python list length

Python array length

Python integer length

Python set length

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.