Write a program to get the last part of a string before a specified character.Note: Specified characters are "/" and "-"Input format :Input consists of a string.Output format :The first line of the output depends on the "/" character.The second line of the output depends on the "-" character.Sample test cases :Input 1 :https:/admin.app.placementseason.com-examlyOutput 1 :https:https:/admin.app.placementseason.comInput 2 :https://www.onlinegdb.com/online_python_compilerOutput 2 :https://www.onlinegdb.comhttps://www.onlinegdb.com/on
Question
Write a program to get the last part of a string before a specified character.Note: Specified characters are "/" and "-"Input format :Input consists of a string.Output format :The first line of the output depends on the "/" character.The second line of the output depends on the "-" character.Sample test cases :Input 1 :https:/admin.app.placementseason.com-examlyOutput 1 :https:https:/admin.app.placementseason.comInput 2 :https://www.onlinegdb.com/online_python_compilerOutput 2 :https://www.onlinegdb.comhttps://www.onlinegdb.com/on
Solution
Here is a Python program that fulfills your requirements:
def get_substring(input_string):
# Split the string on "/" and get the part before the last "/"
split_on_slash = input_string.rsplit('/', 1)[0]
# Split the string on "-" and get the part before the last "-"
split_on_dash = input_string.rsplit('-', 1)[0]
return split_on_slash, split_on_dash
# Test the function with the provided inputs
input1 = "https:/admin.app.placementseason.com-examly"
input2 = "https://www.onlinegdb.com/online_python_compiler"
print(get_substring(input1))
print(get_substring(input2))
This program defines a function get_substring that takes an input string, splits it on "/" and "-", and returns the parts of the string before the last occurrence of these characters. The rsplit function is used with the parameter 1 to split the string at the last occurrence of the specified character. The [0] index is used to get the first part of the split string, which is the part before the last occurrence of the character.
The function is then tested with the provided input strings. The results are printed to the console.
Similar Questions
The library function used to find the last occurrence of a character in a string is
What is the slice expression that gives every third character of string s, starting with the last character and proceeding backward to the first?
What is a correct syntax to return the first character in a string?
For the program segment below, select the values for WWW, XXX, YYY and ZZZ that will give a program segment that will print every character of str1 matched up with every character of str2 to give output:A1A2A3B1B2B3C1C2C3D1D2D3E1E2E3String str1 = "ABCDE";String str2 = "123";int num = 0;while ( WWW ) { int val = 0; while ( XXX ) { System.out.print(str1.charAt(num)); System.out.print(str2.charAt(val)); YYY; } ZZZ;}System.out.println();
Write a program for reading a position and deleting a character at that specified location in a given string
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.