
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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']
- Related Articles
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- How to rotate a matrix of size n*n to 90 degree using C#?
- Python program to right rotate a list by n
- Split String of Size N in Python
- Python Program to calculate n+nm+nmm.......+n(m times).
- Python program to rotate doubly linked list by N nodes
- Python program to randomly create N Lists of K size
- Python program to left rotate the elements of an array
- Calculate n + nn + nnn + … + n(m times) in Python program
- Program to find modulus of a number by concatenating n times in Python
- Calculate n + nn + nnn + u + n(m times) in Python Program
- String slicing in Python to rotate a string
- Program to find number of items left after selling n items in python
- How to return a string repeated N number of times in C#?
- Program to find kth lexicographic sequence from 1 to n of size k Python

Advertisements