Using Python 3 to remove 1st or last line in multi line string

To remove 1st line, change multi line string to list and use list slicing to remove 1st line, see code below:

#this python3 script removes 1st line from a multiline string
multiline_string = "test1\ntest2\ntest3"
print(multiline_string)

num_of_lines = multiline_string.count("\n") + 1
print("number of lines in string : " + str(num_of_lines))

#Split the str at newline and use list slicing to drop 1st line
str_in_list = multiline_string.split("\n")[1:]
print(str_in_list)

#after removal, change list to string
new_str = ""
for item in str_in_list:
    new_str += item
    new_str += '\n'

print(new_str)

Output before and after removal of 1st line.

To remove last line, change multi line string to list and use list slicing to last 1st line, see code below:

#this python3 script removes last line from a multiline string
multiline_string = "test1\ntest2\ntest3"
print(multiline_string)

num_of_lines = multiline_string.count("\n") + 1
print("number of lines in string : " + str(num_of_lines))

#Split the str at newline and use list slicing to drop last line
str_in_list = multiline_string.split("\n")[:num_of_lines-1]
print(str_in_list)

#after removal, change list to string
new_str = ""
for item in str_in_list:
    new_str += item
    new_str += '\n'

print(new_str)

Output before and after removal of last line.