String Length in Python

A string is a collection of characters which are enclosed in quotes.

Sometimes there is a need to find the number of characters in a string.

Use len() to find the number of characters in a string

This function gives the number of characters including spaces

Code : Accept input from keyboard and display number of characters in a string

Use input() to accept data from keyboard

str = input('Enter a string: ')

Create a variable and assign the result obtain by using len()

l = len(str)

Now the display the value of variable which contains count for a number of characters in a string

print(l)

Output

Enter a string: Python
6

Complete Code

str = input('Enter a string: ')
l = len(str)
print(l)
Advertisement

Leave a Comment

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 )

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