Question: Write a function countlines(txt) to return the number of lines in the text string txt! If the text string has any newline characters at the

Write a function countlines(txt) to return the number of lines in the text string txt!

If the text string has any newline characters at the beginning or at the end, do not count these.

Use the docstring """Counts lines in the text string txt""".

Do not change the main program. Expected output:

Computer programming is an art,
because it applies accumulated knowledge to the world,
because it requires skill and ingenuity,
and especially because it produces objects of beauty.
- Donald Knuth
 
The text string has 5 lines.

(Donald Ervin Knuth is a famous American computer scientist, mathematician, and professor emeritus at Stanford University. He is the author of the multi-volume work The Art of Computer Programming, and of the legendary typesetting system TeX.)

# Main program (do not change): quote = "Computer programming is an art, because it applies accumulated knowledge to the world, because it requires skill and ingenuity, and especially because it produces objects of beauty. - Donald Knuth" print(quote, ' The text string has', countlines(quote), 'lines.')

In your function, first insert the docstring.

Then strip trailing spaces and newline characters from both the beginning and end of the text string.

Save the cleaned text string in the variabletxtagain.

Count the number of newline characters intxt, and return this number plus one.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!