How to get the length of a string in Python

Get the length of a string in Python

In Python, you can get the length of a string using the len() function.

The len() function returns the length of a string with whitespace(s)

Getting the length of a string in Python using len() function

1. Create a variable named str_variable and assign string value "Python is fun." to the variable.

2. Use the len(str_variable) function to get the length of the string variable str_variable. Assign the result to a new variable called string_length and get the output using print() function.

Please take a closer look at the following example:

Example of using len() function

Output of the above example
The length of the string is 14

Live Code Playground

In the following Python code editor, you can practice the above code by creating more string variables and getting their length using Python built-in len() function. Click on the 'Execute Code' button to execute the code; the executed output will be displayed in the following Python Code Output frame. Practice until you become comfortable and proficient with your code.

Python logo Python Online Editor
Python Code Output