Question

In: Computer Science

PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index...

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.

Solutions

Expert Solution

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.


Related Solutions

Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1 small, 2 medium, 3 large), one for the number of meat toppings, one for the number of other toppings and another for the quantity of pizzas ordered. It should calculate and return the total due for that pizza order based on the following information: Small pizza base price: $6.50 Medium pizza base price: $9.50 Large pizza base price: $11.50 The base pizza price includes...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT