2

I try to count characters in result of textarea. It can contains many lines.

text = '''1 2 3''' 

if I use len(text) the result is 7, because it counts each newline as two characters (which is quite possible CR+LF are two characters). But I want to count it as one character - is there any method or I have to count newlines and substract it?

3
  • 5
    Perhaps replacing the \r\n with \n might simplify it and make it more cross platform. Commented Dec 25, 2021 at 9:41
  • I'm not sure if it is secure to change form input - what if browser requires cr+lf? Or you mean not to change text just analyze its clone with replaced \r\n Commented Dec 25, 2021 at 12:28
  • Yeah, I meant only for counting. I think your solution is fine from a performance perspective. Commented Dec 25, 2021 at 12:44

1 Answer 1

1

For now I use:

number_of_chars = len(text)-text.count('\r') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.