r/pythonhelp • • Aug 01 '21

how to remove uneseccary lines form a a string?

say i have a string that looks like this:

"hi hi hi

hi hi hi"

And i want to chop off the bottom row hi hi hi so it looks like just this:

"hi hi hi"

3 Upvotes

1 comment sorted by

4

u/MrPhungx Aug 01 '21

I'm pretty sure there are a lot of ways to do so. Here are two

index = string.find('\n')
print(string[:index])

It finds the Index of the first new line and prints everything Up to that position.

parts = string.split('\n')
print(parts[0])

This splits the string at all new line chars and simply prints the first one