Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:09:31+00:00 2026-05-27T02:09:31+00:00

I am trying to change a variable (lett) to the next letter in the

  • 0

I am trying to change a variable (lett) to the next letter in the alphabet on each iteration of a loop. I have to also be able to set this variable at the beginning of the script to a certain letter (and the initial letter will vary depending on the use of the script). I started with creating the following bit of code:

Initial script (when I was still learning):

while lett_trig == 2:
    if set_lett == 2:
        lett = "a"
    if set_lett == 3:
        lett = "b"
    if set_lett == 4:
        lett = "c"
    if set_lett == 5:
        lett = "d"
    if set_lett == 6:
        lett = "e"
    if set_lett == 7:
        lett = "f"
    if set_lett == 8:
        lett = "g"
    if set_lett == 9:
        lett = "h"
#... and this goes on till it reaches if set_let == 27: lett = "z"

    set_lett += 1
    if set_lett == 28:
        set_lett = 2
    print lett

# set_lett starts at two because I left set_lett == 1 to create a space (" ")

This is of course a simplification of a larger script.
This is the only simplification I could come up with:

lett_trig = 2
x = 0

a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

while lett_trig == 2:
    lett = a[x]
    x += 1
    if x == 26:
        x = 0

Is there any other way to mathematically change from one letter to another? Through some binary conversion operation? or is the list-way the most efficient?


Answer: after going through all the answers and testing them for efficiency, I found the dict function to be the fastest (and cleanest). An example of the code:

import string

letter_map = dict(zip(string.ascii_lowercase, string.ascii_lowercase[1:] + string.ascii_lowercase[0]))
lett1 = "d"

while ord(lett2) < 122:
    print lett1
    lett1 = letter_map[lett1]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:09:32+00:00Added an answer on May 27, 2026 at 2:09 am

    Use itertools.cycle:

    import itertools
    import string
    letters = itertools.cycle(string.lowercase)
    

    With this, letters is an infinite sequence of letters, running from a to z repeatedly. You can use this in a while loop by calling letters.next() or in a for loop by imposing a termination condition in some fashion, e.g., itertools.islice.

    You can put this together into a function:

    def cyclic_letters(start='a'):
        i = string.lowercase.index(start)
        letts = string.lowercase[i:] + string.lowercase[:i]
        return itertools.cycle(letts)
    

    The cyclic_letters function also allows the initial letter in the sequence to be selected, defaulting to 'a'.

    Alternatively, you could use a dictionary that shows the next letter for any given letter. You can create a dictionary for that, such as by:

    letter_map = dict(zip(string.lowercase, string.lowercase[1:] + string.lowercase[0]))
    

    This is just a dictionary, so using, e.g., letter_map['c'] will produce 'd'.

    In the above, string.lowercase is just a string containing the lowercase letters. The value will depend on your locale. If you only want 'abcdefghijklmnopqrstuvwxyz', regardless of locale, you can substitute string.ascii_lowercase or just give the explicit string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to change a variable further down the program. I have a
I'm getting some weird behavior from IE when trying to change a session variable
I'm trying to change directory in a called bash script. For this I tried
I'm trying to change de value of an input variable (located in the parent
I am trying to set a background color for an ID + Variable but
I'm trying to change the variable in a page using a userscript. I know
I'm trying to change a static variable in the class's constructor. At the start
I am trying to change a static variable inside a DLL, so when the
Im trying to change this code to do reverse geocoding as well. Coordinates work
I'm using Prototype and trying to dynamically access a variable in a loop. Code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.