Count all characters python

Get the length of a string (number of characters) in Python

In Python, you can get the length of a string str (= the number of characters) using the built-in len() function.

See the following article on how to count the number of specific characters/substrings in a string, rather than the length of the entire string.

See the following article for the usage of len() for other types, such as list .

Get the length of a string (number of characters) with len()

By passing a string to the built-in len() function, its length (the number of characters) is returned as an integer value.

Full-width and half-width characters

Both full-width and half-width characters are treated as one character (length: 1).

s = 'あいうえお' print(len(s)) # 5 s = 'abcdeあいうえお' print(len(s)) # 10 

Escape sequences and special characters

In Python, special characters such as TAB are represented with a backslash, like \t . The backslash itself is represented by \\ . These special characters are considered as single characters.

s = 'a\tb\\c' print(s) # a b\c print(len(s)) # 5 

In raw strings where escape sequences are not treated specially, the string is considered as-is without being interpreted as special characters. The number of characters is also counted as-is.

s = r'a\tb\\c' print(s) # a\tb\\c print(len(s)) # 7 

The Unicode escape sequence \uXXXX is also treated as a single character.

s = '\u3042\u3044\u3046' print(s) # あいう print(len(s)) # 3 

Unicode escape sequences are not treated as special characters in raw strings.

s = r'\u3042\u3044\u3046' print(s) # \u3042\u3044\u3046 print(len(s)) # 18 

Line breaks

\n (LF: Line Feed) is also considered as a single character.

s = 'a\nb' print(s) # a # b print(len(s)) # 3 

If \r\n (CR: Carriage Return + LF: Line Feed) is used, it is counted as two characters, \r and \n .

s = 'a\r\nb' print(s) # a # b print(len(s)) # 4 

If \n and \r\n are mixed, the number of characters in each newline section is different.

s = 'abc\nabcd\r\nab' print(s) # abc # abcd # ab print(len(s)) # 12 

To handle mixed \n and \r\n , or when unsure which one is used, use the splitlines() method, which returns a list split by lines.

print(s.splitlines()) # ['abc', 'abcd', 'ab'] 

The number of elements in the list returned by splitlines() corresponds to the number of lines.

Читайте также:  Python github get token

You can obtain the number of characters in each line using list comprehensions.

print([len(line) for line in s.splitlines()]) # [3, 4, 2] 

To calculate the total number of characters, use sum() with a generator expression. Generator expressions are enclosed in () instead of [] , but when used within () , as in this example, () can be omitted.

print(sum(len(line) for line in s.splitlines())) # 9 

For more information about handling line breaks, see the following article.

  • Pad strings and numbers with zeros in Python (Zero-padding)
  • Convert a list of strings and a list of numbers to each other in Python
  • Right-justify, center, left-justify strings and numbers in Python
  • Convert a string to a number (int, float) in Python
  • Write a long string on multiple lines in Python
  • Regular expressions with the re module in Python
  • Extract a substring from a string in Python (position, regex)
  • Replace strings in Python (replace, translate, re.sub, re.subn)
  • How to slice a list, string, tuple in Python
  • Count characters and strings in Python
  • Wrap and truncate a string with textwrap in Python
  • Sort a list, string, tuple in Python (sort, sorted)
  • Search for a string in Python (Check if a substring is included/Get a substring position)
  • Convert and determine uppercase and lowercase strings in Python
  • How to use regex match objects in Python

Источник

Count characters and strings in Python

This article explains how to count the number of specific characters (letters) or substrings within a string ( str ) in Python.

For details on how to read a text file as a string, calculate the length (the total character count) of a string, or search for a substring within a string, please refer to the following articles:

Count characters and substrings: count()

The count() method allows you to count the number of specific characters or substrings within a string.

s = 'abc_aabbcc_abc' print(s.count('abc')) # 2 print(s.count('a')) # 4 print(s.count('xyz')) # 0 

If the second argument start and the third argument end are specified, the range of the slice [start:end] is targeted.

print(s.count('a', 4, 10)) # 2 print(s[4:10]) # aabbcc print(s[4:10].count('a')) # 2 

Like slicing, a negative value can specify a position from the end. If end is omitted, the range is up to the end.

print(s.count('a', -9)) # 2 print(s[-9:]) # abbcc_abc print(s[-9:].count('a')) # 2 

count() only counts non-overlapping occurrences of the specified substring. Each character is counted only once.

s = 'abc_abc_abc' print(s.count('abc_abc')) # 1 

To count overlapping substrings, use the regular expression described below.

Count the number of words

For example, if you want to count «am» with the count() method, «Sam» is also counted.

s = 'I am Sam' print(s.count('am')) # 2 

To tally specific words, you can use the split() method, dividing the string into a list of words using a specified delimiter, such as spaces or punctuation. You can then use the count() method on the list to count exact word matches.

l = s.split() print(l) # ['I', 'am', 'Sam'] print(l.count('am')) # 1 

For long sentences, the Counter class of the standard Python library collections is useful for counting the frequency of each word. See the following article.

Читайте также:  Php html header cache control

Keep in mind that using split() to divide a string into words is a basic approach. Since actual sentences may contain various symbols, it is safe to use a natural language processing library such as NLTK.

Count with regex: re.findall()

Use re.findall() to count substrings that match a regex pattern.

re.findall() returns a list of all substrings that match the pattern. Use the built-in len() function to get the total count of matched substrings.

import re s = '123-456-789' print(re.findall('2 ', s)) # ['123', '456', '789'] print(len(re.findall('6 ', s))) # 3 

In the example above, 8 is a regex pattern matching any three-digit number.

You can also count overlapping substrings using a lookahead assertion (? =. ) and grouping () .

s = 'abc_abc_abc' print(re.findall('(?=(abc_abc))', s)) # ['abc_abc', 'abc_abc'] print(len(re.findall('(?=(abc_abc))', s))) # 2 s = '12345' print(re.findall('(?=(6 ))', s)) # ['123', '234', '345'] print(len(re.findall('(?=(8 ))', s))) # 3 

For more information on the re module, see the following article.

Case-insensitive counting

s = 'abc_ABC' print(s.count('abc')) # 1 

For case-insensitive counting, you can convert the string to upper or lower case. Use upper() to make a string all uppercase and lower() to make it all lowercase.

print(s.lower()) # abc_abc print(s.lower().count('abc')) # 2 print(s.upper()) # ABC_ABC print(s.upper().count('ABC')) # 2 

With regex, you can set re.IGNORECASE as the flags parameter in functions like re.findall() for case-insensitive counting.

print(re.findall('abc', s, flags=re.IGNORECASE)) # ['abc', 'ABC'] print(re.findall('ABC', s, flags=re.IGNORECASE)) # ['abc', 'ABC'] 

Источник

Count number of characters in a string in python

Count the number of characters in a String in Python

In this post, we will see how to count number of characters in a String in Python.

We can think of strings as a collection of characters, with every character at a given index.

Ways to count the number of characters in a string in Python

In this tutorial, we will find out the total number of characters in a string in Python.

Using the len() function

This function is the most straightforward method. The len() function returns the length of a given iterable in Python. We can use it to find the number of characters in a string.

Further reading:

How to compare String in Python
Loop through String in Python

Using the for loop

We can use the for loop to iterate over a string in Python. We can use a counter variable and increment it in every iteration. This variable will return the total number of characters in a string.

  • The t variable is given a value of 0.
  • We iterate over the string s using the for loop.
  • In every iteration, we increment t and display its value after the loop ends.

Using the collections.Counter class

The collections.Counter class stores the elements of a string as key-value pairs. The keys are the characters of the string, and the value of each key is how many times this character occurs in the string.

We can sum up these values to find the total number of characters in the given string.

  • We create an object of the Counter class ob .
  • We create an object of all the values of the dictionary-like object ob with the values() function.
  • The sum() function returns the sum of these values.

Conclusion

In this tutorial, we discussed how to get the characters in a given string in Python. The len() function is the simplest and most used method. We can also use the for loop and Counter class for a lengthy method.

Was this post helpful?

You may also like:

Bash Get Output from Python Script

Count Unique Values in NumPy Array

Create Array of Arrays in Python

Convert String List to Integer List in Python

Convert Object to Float in Pandas

NameError: Name requests Is Not Defined

Python Hex to String

NameError: Name xrange Is Not Defined in Python

Call Python Script from Bash with Arguments

TypeError: ‘dict_values’ Object Is Not Subscriptable

Share this

Author

How to decrement for loop in Python

How to decrement for loop in python

Table of ContentsWhat is for loop in Python?Ways to decrement the for loop in PythonUsing the start, stop and step parameters in range() functionUsing the reversed() functionUsing the while loopConclusion We use the for loop widely in the programming world. Most programs, whether highly complex or not, contain this loop. Sometimes, depending on the conditions, […]

Calculator program in Python

Calculator program in Python

Table of ContentsUsing the while loop along with the if. else conditional statement.Define functions for Addition, Subtraction, Multiplication and DivisionTake user input using input functionComplete calculator program in Python A simple Calculator can be utilized to carry out the four basic arithmetic operations namely addition, division, multiplication, and subtraction depending on the input of the user. […]

Number guessing game in Python

Table of ContentsNumber guessing game RulesNumber guessing game implementation in PythonJava implementation A number guessing game is a common mini-project for basic programmers who have a grasp on random number generation and conditional statements with iteration. The number guessing game is based on a concept where player has to guess a number between given range. […]

Perfect number in Python

Perfect number program in Python

Table of ContentsUse the Simple iteration method to check whether a given number is a perfect number.Use the square root method to check whether a given number is a perfect number. According to number theory, a limb of pure mathematics that deals with integers, a Perfect Number can be defined as a positive integer whose […]

Источник

Оцените статью