How do you find the length of a string in Python without needing to import libraries?
To find the length of a string in Python use the built-in
function which takes one parameter that can be of data type string, bytes, tuple, list, or range; or a collection (such as a dictionary, set, or frozen set). The
function when used on a string will return the number of characters in that string.
built-in function is an abbreviation of the word
Here’s an example demonstrating the use of this function in the Python REPL:
>>> my_string = "How long is a piece of string?" >>> len(my_string) 30
As you can see from the above example, I have assigned a string to a variable I’ve labelled
then I use the built-in
function and insert the name of the variable into the parameter of the
function to find the total number of characters in that string.
It’s important to note that the
function counts the number of characters in the string when it is used on a string data type.
Each element within a string is comprised of
. To know if a character will be recognised in Python use the
by inserting the character into its single parameter. If the
function returns a number then the character will be recognised
If the data type is something other than a string or byte or tuple or list or range or collection then you will get a
. For example, if you insert a number or boolean data type you will get a
Here’s an example of what this would look like in the Python REPL:
>>> len(123456) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'int' has no len() >>> len(True) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'bool' has no len()
As you can see from the above examples when an
data type is used in the
function it returns a
, similarly with the next example where a
data type is used.
How Do You Get The Length Of A Number?
As demonstrated above you cannot use the
function when the data type of the parameter used for
is a number, but there is a simple workaround: convert the number to a string first.
To convert a number to a string, use another built-in function called
, which takes either one parameter or three parameters. I’ll be using the
in this case, which takes only one parameter.
Here’s an example of how to find the length of a number using the
>>> my_int = 123456 >>> len(str(my_int)) 6
As you can see from the above example, I can easily convert the
data type variable in the
function and wrap that with the
The result is the length of the number.
Is there an alternative way to count the length of a string without using the
To count the number of characters in a string without using the
function you could use a
with the built-in
function which would allow you to write an alternative to
that would still be a one-liner.
Here’s an example demonstrating the
function as an alternative to
>>> my_string = "How long is a piece of string?" >>> sum([1 for x in my_string]) 30
As you can see from the above example the list comprehension produces a list of
for each character in the
variable and the
function totals all the elements in the list producing the same result as before.
The beauty of this approach is that it allows you to filter out characters that you may not want to count.
Count Length Of String Excluding Space Character
If you want to filter the string to exclude characters, such as the space character, you can either create a new string by modifying the original string and then insert the new string into the
function, or you could use the above
and list comprehension approach and apply your filter after the for loop.
Here’s an example demonstrating both approaches:
>>> my_string = "How long is a piece of string?" >>> no_spaces = my_string.replace(' ', '') >>> print(no_spaces) Howlongisapieceofstring? >>> len(no_spaces) 24
Using the first method where the second variable contains the space-less original string by using the built-in
function the approach is fairly straightforward.
Here’s an example demonstrating the
and list comprehension technique:
>>> my_string = "How long is a piece of string?" >>> sum([1 for x in my_string if ord(x) != 32]) 24
With this type of approach as displayed from the Python REPL output above all that has been added is the filter
if ord(x) != 32
. This condition checks the
in the string and if it represents the same ordinal character as the space character, being 32, then it does not add
to the list.
This approach may also be handy if there are other characters you wish to exclude from being in the count such as carriage returns or line feeds.
If you wanted to expand the list of characters to exclude, such that new line or carriage returns were also removed you would modify the
list comprehension approach to be something like this:
sum([1 for x in my_string if ord(x) not in [10, 13, 32]])
This would then exclude any new line, return and space characters from being added to the count of characters in your original string.
To count the number of characters in a string use the built-in method
which counts each character contained in the string provided the character is recognised by Python.
function will return a
if the variable inserted into the function is of a data type that does not permit iteration, such as
An alternative approach to using the
function that can permit further filtering on the original string without modifying the original string is to use the
function with a list comprehension.