Finding length of a string

String is a group of characters. Python has built in function which calculates number of characters in a string. We can use len() function to find the length of a string. Space in a string is also considered as a character.

In this blog, we will ask user to enter any string then using the len() function to find the number of characters in a string.
Asking user to input any string. Using str variable to store the string entered by user
str = input('Please enter any string: ')
Using len(str) function to find number of characters in a string and assigning the value to l variable
l = len(str)
Displaying the result
print('Total length a string:%s'%l)
OUTPUT
Please enter any string: Pyhton is a programming language
Total length a string:32
Complete Code
str = input('Please enter any string: ')
l = len(str)
print('Total length a string:%s'%l)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s