Python program to count the number of characters in a String


Strings in Python can contain any sequence of characters, including letters, numbers, symbols, and whitespace. It can represent text, numbers, or any other type of data that can be represented as a sequence of characters.

In Python, strings are enclosed in single quotes ('') or double quotes (""). Multiline strings can be created using triple quotes (''' or """). Here are a few examples of valid Python strings:

  • "Hello, World!"

  • "12345"

  • "Python is awesome!"

Counting the number of characters in a string involves determining the total count of individual characters within the string. Python offers various techniques for character counting −

  • String Length − The simplest method is to use the length function available in most programming languages. It returns the total number of characters in the string.

  • Iteration − Another approach is to iterate over each character in the string and increment a counter for each non-whitespace character encountered. This method allows for additional operations or checks on each character while counting.

  • Regular Expressions − Regular expressions provide powerful pattern-matching capabilities. They can be used to identify and count specific types of characters or patterns within a string.

In this article, we will discuss the mentioned techniques to count the number of characters in a String.

Using the len() function

In this approach, we will use the Python built-in len() function to return the total number of characters in the string.

Example

Here is an example, that counts the number of characters in a string using the len() function.

# Define the input string
input_string = "***Tutorialspoint!***"
print("Input string:", input_string)

# Count the characters using len() function
character_count = len(input_string)
print("Number of characters:", character_count)

Output

Input string: ***Tutorialspoint!***
Number of characters: 21

Example

In this example, we will count only alphabet characters using len() and isalpha() functions.

# Define the input string
input_string = "***Tutorialspoint!***"
print("Input string:", input_string)

# Count only alphabet characters using len() and isalpha() functions
character_count = len([character for character in input_string if character.isalpha()])
print("Number of characters:", character_count)

Output

Input string: ***Tutorialspoint!***
Number of characters: 14

Using a loop

In this approach, we'll start by initializing a count variable to keep track of the number of characters. Next, we'll iterate through each character in the string. During the iteration, if a character is identified as an alphabet, we'll increase the count by 1.

Example

Here we will only count the alphabets using the isalpha() method.

def count_characters(string):
    count = 0
    for char in string:
        if char.isalpha():
            count += 1
    return count

# Define the input string
input_string = "***Hello, World!***"
print("Input string:", input_string)

result = count_characters(input_string)
print("Number of characters:", result)

Output

Input string: ***Hello, World!***
Number of characters: 10

Using the regular expression

The re.findall() function is used to count the number of characters in the input string. The regular expression pattern r'\S' matches any non-whitespace character.

Example

Here is an example using the regular expression to count the number of characters in the input string.

import re

# Define the input string
input_string = "***Hello, World!***"
print("Input string:", input_string)

# Count characters using re.findall() function
character_count = len(re.findall(r'\S', input_string))
print("Number of characters:", character_count)

Output

Input string: ***Hello, World!***
Number of characters: 18

To count only the alphabetic characters, the regular expression pattern r'[A-Za-z]' should be used.

Updated on: 29-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements