Close Menu
    Facebook X (Twitter) Instagram
    Trending
    • SharePoint Internal Column Name Encoded Character List
    • Satchel Pelts List for Red Dead Redemption 2
    • Create a Video Wall With FFmpeg
    • Mitsubishi Outlander Towbar Electrics
    • Mitsubishi Outlander Towbar Installation
    • Telephone Block List
    • Hard Drive Repair After Wrong Voltage Mishap
    • How To Send Excel Workbook As Email Using Button
    YouTube Instagram Facebook RSS
    Technology Spy
    • Tutorials & Help
    • Reviews
    Technology Spy
    You are at:Home»Development»Python»How To Rotate The Characters In A Text String Using Python
    Coding Programming Guides

    How To Rotate The Characters In A Text String Using Python

    0
    By Matt on May 28, 2014 Python, Tutorials & Help

    While working on some code for an 16×2 LCD display on my Raspberry Pi I wanted a way to rotate a text string in a specified direction and by a specified number of characters. The idea was to use it to scroll text across the screen.

    To do this I created the function below. It takes a text string and rotates the characters based on the specified arguments. For basic use you only need to feed it a text string. This can be any length.

    Here is the code. It includes a function definition followed by three examples of its use.

    def RotateMe(text,mode=0,steps=1):
      # Takes a text string and rotates
      # the characters by the number of steps.
      # mode=0 rotate right
      # mode=1 rotate left
      length=len(text)
    
      for step in range(steps):
      # repeat for required steps
    
        if mode==0:
          # rotate right
          text=text[length-1] + text[0:length-1]
        else:
          # rotate left
          text=text[1:length] + text[0]
    
      return text
    
    # Rotate right 3 characters
    message=RotateMe("how2code",0,3)
    print message
    
    # Rotate left 2 characters
    message=RotateMe("how2code",1,2)
    print message
    
    # Rotate using defaults
    message=RotateMe("how2code")
    print message
    

    The function RotateMe accepts three arguments.

    • text is the string of characters.
    • mode determines the direction of the rotate. 0 for right and 1 for left. If you don’t specify a value it defaults to 0.
    • steps determines the number of characters to rotate. If you don’t specify a value it defaults to 1.

    When you run it you should see the output :

    odehow2c
    w2codeho
    ehow2cod
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow To List Files In Python Filtered By Age And Size
    Next Article Welcome

    Related Posts

    Create a Video Wall With FFmpeg

    Mitsubishi Outlander Towbar Electrics

    Mitsubishi Outlander Towbar Installation

    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    109

    Recent Posts
    May 1, 2025

    SharePoint Internal Column Name Encoded Character List

    March 24, 2025

    Satchel Pelts List for Red Dead Redemption 2

    January 29, 2025

    Create a Video Wall With FFmpeg

    Categories
    • 3D Printing
    • Android
    • Arduino
    • Development
    • ESP8266
    • Excel
    • Gaming
    • General
    • GIMP
    • Home Automation
    • JavaScript
    • Linux
    • Microsoft Office
    • Mobile Devices
    • Oracle APEX
    • Python
    • Raspberry Pi
    • Reviews
    • Security
    • SharePoint
    • Tutorials & Help
    • VBScript
    Web Tools

    A set of quick and basic online tools for web designers and software developers :

    • Hash Generator
    • Text to HTML List
    • Text to HTML Table
    • URL Encoder and Decoder
    • UNIX Timestamp Calculator
    • LED Resistor Calculator
    • Extract Email from Text
    • Mortgage Calculator
    Tags
    Android APEX Apple Arduino Black Friday Coolermaster csv Elite Dangerous email ESP-01 ESP8266 EV Excel file handling Format gaming GIMP Gritin Home Assistant Home Automation Linux lists Media os.stat os.walk Outlander Power Python Qi RDR2 Review scam Security SharePoint 2010 string Syncwire text text files TrueCrypt Ubuntu USB-C VBA VBscript Windows 10 Xbox One
    About

    Welcome to Technology Spy, a site devoted to gadgets, computers, programming and all things technology! You’ll also find product reviews for items I own as well as tutorials, guides and scripts for the software I use.

    Archives
    Other Resources
    • MattsBits
    • Raspberry Pi Spy
    YouTube Facebook Instagram Pinterest RSS

    Entries RSS | Comments RSS

    Copyright © 2025 - All Rights Reserved - Matt Hawkins

    mastodon.social@MattHawkins

    Type above and press Enter to search. Press Esc to cancel.