How to count len() without \n? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

How to count len() without \n?

29th Jan 2021, 5:07 AM
Oleg Avrah
2 Respuestas
+ 5
Use str.strip() to remove whitespace from the string. " test \n".strip() results in "test"
29th Jan 2021, 5:34 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
OR if there are "\n" in middle of string for example: x = "Hello \n World" (This can't be removed by strip) So here, use string.replace() method: x = x.replace("\n", "") Parameters: x.replace(old_char, new_char) Then len(x)
29th Jan 2021, 7:03 AM
noteve
noteve - avatar