Python program to calculate length of a string

By Lenin Mishra

Method 1 - Simple for loop

Code

user_input = input("Provide a string ==> ")
length = 0
for char in user_input:
    length += 1
print(f"Length of '{user_input}' is {length}")

Output

Provide a string ==> Joe Biden
Length of 'Joe Biden' is 9

Method 2 - Using len() built-in function

Code

user_input = input("Provide a string ==> ")
print(f"Length of '{user_input}' is {len(user_input)}")

Output

Provide a string ==> Pylenin
Length of 'Pylenin' is 7
  1. f-strings in Python
  2. How input() function works in Python
  3. Common Python String Methods