Count the number of characters in a String - Python
Last Updated :
15 Jul, 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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. OOPs is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOPs, object has attributes thing th
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython's input() function
7 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read