In: Computer Science
PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index (n).
The function will:
1.Remove the n-th characterof str1 (the initial character is the 0-th character)
2.Swap the order of the substring in front of the removed character and the substring afterwards, and concatenate them as a new string
3.Return the converted (str2) back to the function caller For example, myfunc(“abc1xyz”, 3) returns “xyzabc”.
Additional Features [6 pts]
Expand the function by adding more features:
4.If str2contains character @, keep all characters in front of @, store them in a new string (str3),and dropthe rest
5.If str3 has 5 or morecharacters, keep the last 5(“abcdef” => “bcdef”. If str3 has 4 characters or less, add an appropriate number of underscore character (“_”) sothat the resulting string has exactly 5 characters.
6.Return the final string back to the function caller.
Program
def myfunc(str1, n):
#str2 is swapped substring
#concatenation
str2 = str1[+1:]+str1[:n]
#if @ is present in str2
if '@' in str2:
#getting substring upto @
str3 = str2[:str2.find('@')]
#if str3 length is greater
#than or equal to 5 then
#rerurning the last 5 chars
if len(str3)>=5:
return str3[-5:]
#else returning _ padded upto
#5 chars
else:
return str3+'_'*(5-len(str3))
#if str2 doesn't have any @
#rerurning it directly
return str2
#sample tests
print(myfunc("abc1xyz", 3))
print(myfunc("@hello.comworld", 6))
print(myfunc("@hello.hi", 6))
Program Screenshot
Output Screenshot
Each and everything is explained within the content section of the code.
Thank you! Hit like if you like my work.