Count the number of characters in a String - Python
Last Updated :
26 Apr, 2025
The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Let’s explore different approaches to accomplish this.
Using len()
len() is a built-in method that returns the number of elements in a sequence, such as a string. It directly accesses the internal length attribute of the string, making it the most efficient way to count characters.
Python
s = "GeeksForGeeks"
res = len(s)
print(res)
Explanation: len() returns the length of the string (13 in this case) and assigns it to the variable res, which is then printed.
Using generator expression
Generator expression like sum(1 for _ in s) creates a lazy iterator that yields 1 for each character. The sum() function then adds these 1s, giving the total character count. This method is Pythonic, clean and allows for easy filtering to customize the counting.
Python
s = "Geeks for Geeks!"
res = sum(1 for _ in s)
print(res)
Explanation: sum(1 for _ in s) count the characters in the string s. For each character in the string, it yields 1 and the sum() function adds these 1s together.
Using for loop
This method manually loops through each character in the string, incrementing a counter by 1. It’s simple, readable and easy to modify for more complex logic like conditional counting.
Python
s = "Geeks for Geeks!"
count = 0
for char in s:
if char != ' ':
count += 1
print(count)
Explanation: This code initializes count to 0 and iterates over each character in s. If the character is not a space (char != ' '), count is incremented. After the loop, count contains the total number of non-space characters in the string.
Using reduce()
reduce() function from functools module applies a function cumulatively to items in a sequence. You can use it to count characters by accumulating 1 for each character, but it's less readable and efficient than loops or built-in functions due to function call overhead.
Python
from functools import reduce
s = "GeeksForGeeks"
res = reduce(lambda acc, _: acc + 1, s, 0)
print(res)
Explanation: This code uses reduce() with a lambda function to increment an accumulator for each character in s, starting from 0. After processing all characters, it returns and prints the total count.
Related Articles:
Similar Reads
Get Last N characters of a string - Python We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh
2 min read
Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
Frequency of Numbers in String - Python We are given a string and we have to determine how many numeric characters (digits) are present in the given string. For example: "Hello123World456" has 6 numeric characters (1, 2, 3, 4, 5, 6).Using re.findall() re.findall() function from the re module is a powerful tool that can be used to match sp
3 min read
Check if string contains character - Python We are given a string and our task is to check if it contains a specific character, this can happen when validating input or searching for a pattern. For example, if we check whether 'e' is in the string 'hello', the output will be True.Using in Operatorin operator is the easiest way to check if a c
2 min read
Python program to count the number of spaces in string In Python, there are various ways to Count the number of spaces in a String.Using count() Methodcount() method in Python is used to return the number of occurrences of a specified element in a list or stringPythons = "Count the spaces in this string." # Count spaces using the count() method space_co
3 min read
Python program to calculate the number of words and characters in the string We are given a string we need to find the total number of words and total number of character in the given string.For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string. I
3 min read