I am creating a python movie player/maker, and I want to find the number of lines in a multiple line string. I was wondering if there was any built in function or function I could code to do this:
x = """
line1
line2 """getLines(x)
I am creating a python movie player/maker, and I want to find the number of lines in a multiple line string. I was wondering if there was any built in function or function I could code to do this:
x = """
line1
line2 """getLines(x)
If newline is '\n'
then nlines = x.count('\n')
.
The advantage is that you don't need to create an unnecessary list as .split('\n')
does (the result may differ depending on x.endswith('\n')
).
str.splitlines()
accepts more characters as newlines: nlines = len(x.splitlines())
.