Question: In Python: In this problem we will be building a function to rotate a string by a positive integer n that puts the last n

In Python:

In this problem we will be building a function to rotate a string by a positive integer n that puts the last n characters in front of the string.

rotate_string("hello world",3) would return "rldhello wo"

**If the string is empty or a single character, the function should simply return the string unchanged.**

If shift amount is greater than the length of the string, return the difference between shift-len(string) as spaces placed at the beginning of the string.

rotate_string("cat",5) would yield " cat" (cat with 2 spaces) since shift=5, len(string)=3, 5-3=2 therefore 2 spaces get padded to the string on the front

This is an **excellent** opportunity to get comfortable with string slicing (this problem can be solved other ways, but slicing is quite effective!)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!