• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Rotate String in Python

How to Rotate String in Python

June 24, 2022 Leave a Comment

In Python, the easiest way to rotate characters in a string is with slicing. You can rotate a string backwards or forwards with slicing.

string = "hello"

string_rotated_backwards = string[1:] + string[:1]
string_rotated_forward = string[-1:] + string[:-1]

print(string_rotated_backwards)
print(string_rotated_forward)

#Output:
elloh
ohell

In Python, strings are one of the most used data structures. When working with strings, it is useful to be able to change the order of characters of a string in an easy way.

With Python, we can easily rotate the characters in a string both to the right or the left.

To rotate a string backwards, we slice the string from the second character to the end, and then add a slice with only the first character to the end of first slice.

To rotate a string forward, we slice the string from the second to last character to the beginning, and then add a slice with only the last character to the beginning of first slice.

Below is an example of how to rotate a string both backwards and forward with string slicing using Python.

string = "hello"

string_rotated_backwards = string[1:] + string[:1]
string_rotated_forward = string[-1:] + string[:-1]

print(string_rotated_backwards)
print(string_rotated_forward)

#Output:
elloh
ohell

If you need to rotate a string multiple times, we can define a function which rotates the string a specified number of characters.

Below is a function which will rotate the characters in a string using slicing multiple times to the left or right depending on the argument values passed.

def rotateString(string,direction,n):
    if direction == "backwards":
        new_string = string[n:] + string[:n]
    else: 
        new_string = string[-n:] + string[:-n]
    return new_string

print(rotateString("progrmaming","backwards",2))
print(rotateString("progrmaming","forwards",3))

#Output:
ogrmamingpr
ingprogrmam

Hopefully this article has been useful for you to learn how to rotate strings in Python.

Other Articles You'll Also Like:

  • 1.  Remove Element from Set in Python
  • 2.  Replace Forwardslashes in String Using Python
  • 3.  pandas tail – Return Last n Rows from DataFrame
  • 4.  Convert First Letter of String to Lowercase in Python
  • 5.  Using Python to Flatten Array of Arrays
  • 6.  Sort List of Tuples in Python
  • 7.  How to Iterate through a Set Using Python
  • 8.  How to Remove NaN from List in Python
  • 9.  Count Number of Keys in Dictionary in Python
  • 10.  pandas mean – Get Average of Series or DataFrame Columns

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy