 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to rotate a string of size n, n times to left in Python
Suppose we have a string s of size n. We have to find all rotated strings by rotating them 1 place, 2 places ... n places.
So, if the input is like s = "hello", then the output will be ['elloh', 'llohe', 'lohel', 'ohell', 'hello']
To solve this, we will follow these steps −
- res := a new list
- n := size of s
- for i in range 0 to n, do- s := (substring of s from index 1 to n-1) concatenate s[0]
- insert s at the end of res
 
- return res
Example
Let us see the following implementation to get better understanding −
def solve(s): res = [] n = len(s) for i in range(0, n): s = s[1:n]+s[0] res.append(s) return res s = "hello" print(solve(s))
Input
hello
Output
['elloh', 'llohe', 'lohel', 'ohell', 'hello']
Advertisements
 