How to Count Characters in a String

The number of characters in a string can be counted using the len() function in Python.

Example: Counting Characters in a String

# Counting characters in a string
text = "Hello, Python!"
count = len(text)
print(f"The string contains {count} characters.")

Output

The string contains 13 characters.

Explanation: The len() function returns the number of characters in the string, including spaces and punctuation.