I have a multiline string generated by a script that creates ASCII art from an image. It creates a line, then adds \r and keeps going. How do I get the length of the first line, or before it says \r without using regex? Preferably the code is fairly readable.
With find or index?
>>> 'abcfoo\rhahahahaha'.find('\r')
6
>>> 'abcfoo\rhahahahaha'.index('\r')
6
Try:
first, _, _ = s.partition('\r')
k = len(first)
If you don't need the string, you can just use index:
k = s.index('\r')
This works because s.index('\r') contains the lowest index k for which s[k] == '\r' -- this means there are exactly k characters (s[0] through s[k-1]) on the first line, before the carriage return character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With