How do I get the length of the first line in a multi-line string?
Try:
first, _, _ = s.partition('\r') k = len(first)
If you don't need a string, you can simply use index
:
k = s.index('\r')
This works because it s.index('\r')
contains the lowest index k
for which s[k] == '\r'
- it means there are exactly k
characters ( s[0]
thru s[k-1]
) in the first line before the carriage returns a character.
source to share
import string
string.split(yourString, '\r')
length = len(string[0])
So what we have here is straight forward. We take your string and we strip it as soon as we get the / r tag. Then, since all the lines enclosed with / r are in the array, we just count the first captured line in the array and assign the length to var.
source to share