×

Python Program to find the length of a string

Python program to find the length of a string

A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters in a string is known as the length of the string. In this article, we'll look at the various methods for determining the length of a string.

Example:  
Input = “ABC” Output = 3  
Input = “Hello” Output = 5

Method 1: Using built-in method len()

The built-in len() method is the simplest way to calculate a string's length in Python. It takes a string as a parameter and returns the length of that string as an integer.

Source Code

The following source code calculates the length of a string with the use of library functions.

#Program to find the length of a string using len()   
#input string str = input("Enter a string: ")   
#prints the result print(len(str))

Output

find the length of a string

Explanation: A built-in function len() returns the number of characters in a string.

Method 2: Using for loop

The following source takes the input string from the user and counts the number of characters in the input string using for loop.

#Program to find length of a string with for loop 
#input string str = input("Enter a string: ") 
#returns length of string def length(str):     
count = 0     for i in str:         temp += 1     return count 
#prints the result print("Length of the input string is:",len(str))

Output

find the length of a string

Explanation: In the above code, we have declared a length() and pass the argument str. We have used the variable temp in for loop to store the number of iterations of string. When the iteration completes, the length of the string will be printed on the screen.

Method 3: Using while loop and slicing

The following source code counts each character's position in the string by using the string slicing method. The string's length is determined by the final count of the number of positions in the string.

#Program to find length of string using slicing approach 
#return string length def length(String1):     count = 0     while String1[count:]:         count += 1     return count 
#Input string String1 = "TUTORIALANDEXAMPLE" 
#prints the result print("Length of the input string is:",len(String1))

Output

find the length of a string

Explanation: Each iteration of slicing a string shorter by one would probably result in an empty string. This is where the while loop comes to an end. The length of the string can be calculated by keeping the count of the number of iterations.


Related Topics

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python issubclass() Function

Python issubclass() Function The issubclass() function in Python returns a boolean value ‘True’ if the given object is a subclass of classinfo, else it returns False. Syntax issubclass(class, classinfo) Parameter class: It is a required parameter which represents a tuple...

1 minute read.

Get Bounding Box Co-ordinates Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

6 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph --><p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the...

9 minutes read.

Exponentiation in Python

What is an Exponent in Python? Exponent is a fundamental mathematical concept that is used in many different areas, such as engineering, physics, and finance. In mathematics, an exponent is a...

4 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

Recursive Multiplication Python

Recursive Multiplication Python In this tutorial, we will learn how to write a Python program to get the multiplication of two numbers using the recursive approach. In the recursive multiplication approach, we...

2 minutes read.

Python bytearray()

Python bytearray() Class The bytearray() class is used to return a bytearray object which is an array of the specified bytes. It gives a mutable sequence of integers in the range 0...

1 minute read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

Exclusive OR in Python

In Python, the exclusive OR (XOR) operator is represented by the caret symbol (^). It compares each bit of the first operand to the corresponding bit of the second operand,...

3 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.

Python Program to Print all the Prime Number in an Interval

Python Program to Print all the Prime Number in an Interval What is Prime numbers? A prime number is referred to those numbers that can be divisible by them only. In simple words,...

4 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.