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

1

u/socal_nerdtastic Mar 07 '21

That depends on your function.

For a single string you would use the capitalize method:

>>> "i like cats".capitalize()
'I like cats'

1

u/LurkerFinallyJoins Mar 07 '21

Please let me post my function..one moment.

0

u/[deleted] Mar 07 '21

[deleted]

1

u/LurkerFinallyJoins Mar 07 '21

now I just want to add in the bit..where the first letter of each of the lines is capitalized. Any advice is appreciated

1

u/normalrowan Mar 08 '21

Can you post your function?

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)