Part B: Rotate Write a function rotate(s,n) that has one string parameter s foll
ID: 3844958 • Letter: P
Question
Part B: Rotate Write a function rotate(s,n) that has one string parameter s followed by a positive integer parameter n. It returns a rotated string such that the last n characters have been moved to the beginning. If the string is empty or a single character, the function should simply return the string unchanged. Assume that n is less than or equal to the length of s and that n is a positive intger. For example: rotate('abcdefgh',3) returns 'fghabcde' (Optional challenge: write the function to handle n larger than the length of s.)
Use Python
Explanation / Answer
# Hello World program in Python
def rotate(s,n):
if s.strip(): #checking for blank or single charecter
g=s[-n:]
h=s[:-n]
return g+h #returning the new string
else:
return s #returning if string is blank
d = input("Enter the string ") #accepting string from user
a = input("Enter the rotate number ")
k=rotate(d,a)
print k #printing the result