r/pythonhelp • • Mar 07 '21

Capitalization Python: noob question

Hi, thanks for ur help in advance.

I have a string "I like cats and dogs and monkeys".

And I have a function that changes it to this:

"i like cats

and dogs and

monkeys."

How do I get it so the first letter of each line is capitalized? thanks

3 Upvotes

6 comments sorted by

View all comments

1

u/benjotron Mar 08 '21

I would do something like:

s = """"i like cats
and dogs and
monkeys."""

s = "\n".join([capitalize(x) for x in s.split("\n")])

If you find the list comprehension syntax confusing:

lines = s.split("\n")
capitalized_lines = []
for line in lines:
capitalized_lines.append(line.capitalize())
capitalized_s = "\n".join(capitalized_lines)