I have a string, and I have to count all elements in this string.
str = '\r\n\r\n\r\n \r\n \xa0\xa0\r\nIntroduction\r\n\r\n\r\nHello\r\n\r\nWorld\r\nProblems...\r\nHow to calculate numbers...\r\nConclusion\r\n\r\n\r\n\xa0\r\n\r\nHello world.'
These elements contain numbers, letters, escape sequences, whitespaces, commas, etc.
Is there any way to count all elements in this kind of string in Python?
I know that len()
and count()
cannot help. And I also tried some regex methods like re.findall(r'.', str)
, but it cannot find elements like \n
and also can only find \r
instead of \
and r
.
Edit:
To be more clear, I want to count \n
as 2, not 1, and also \xa0
as 4, not 1.