Count the number of characters in a String - Python Last Updated : 26 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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) Output13 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 expressionGenerator 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) Output16 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 loopThis 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) Output14 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) Output13 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:stringlen()Generator expression loopsreduce()functools module Comment More infoAdvertise with us Next Article Count the number of characters in a String - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python 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 Python - Ways to Count Number of Substring in String Given a string s, determine the number of substrings that satisfy certain criteria. For example we are given a string s="hellohellohello" we need to count how many time the substring occur in the given string suppose we need to find the substring "hello" so that the count becomes 3. We can use metho 2 min read Maximum Frequency Character in String - Python The task of finding the maximum frequency character in a string involves identifying the character that appears the most number of times. For example, in the string "hello world", the character 'l' appears the most frequently (3 times).Using collection.CounterCounter class from the collections modul 3 min read Python - Characters which Occur in More than K Strings Sometimes, while working with Python, we have a problem in which we compute how many characters are present in string. But sometimes, we can have a problem in which we need to get all characters that occur in atleast K Strings. Let's discuss certain ways in which this task can be performed. Method # 4 min read Python Program to Count characters surrounding vowels Given a String, the task is to write a Python program to count those characters which have vowels as their neighbors. Examples: Input : test_str = 'geeksforgeeksforgeeks' Output : 10 Explanation : g, k, f, r, g, k, f, r, g, k have surrounding vowels. Input : test_str = 'geeks' Output : 2 Explanation 3 min read Like