How to create a loop from 1-9 and from a-z?

2024/10/5 15:36:17

I currently use:

for i in range(1,10):print i

Which prints the digits 1 to 9. But I want to add a-z to the mix. How can I combine them?

Answer

Rather than create a range, loop over a string, to get individual characters:

import stringfor character in string.ascii_lowercase + string.digits[1:]:print character

This uses the string module to grab pre-defined strings of ASCII lowercase letters and digits.

The string.digits[1:] slice removes the '0' character to leave just the digits '1' through to '9'.

https://en.xdnf.cn/q/119995.html

Related Q&A

Need Python to accept upper and lower case input

I want my Python code to accept both uppercase and lowercase input.Ive tried casefold, but with no luck. Any help?advice ="" while advice !=("Yes"):print("Would you like some…

What is [1] , in sock.getsockname()[1]? [duplicate]

This question already has answers here:How to access List elements(5 answers)Closed 7 years ago.I was going through socket programming in Python and I saw this: sock.getsockname()[1] Can anyone please …

How to use sys.exit() if input is equal to specific number

I am looking to correct this code so that when the user inputs 99999 then the code stops running, im also looking to make it so that if the user input is 999 it sets the total to 0 import sysdef money_…

python about linking sublist with same number together

I need to group sublists with the same elements together For example:list1 =[[1, 0], [2, 1], [30, 32]]would link [1, 0] and [2, 1] together since they both contain 1 and those two would combine into [0…

what to fix to solve the TypeError: an integer is required (got type str) [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 6 years ago.I have a problem with the choice of calculation years. python flux2nc.py ../data/output/fluxes/ …

Multiprocessing in python/beautifulsoup issues

Hi guys im fairly new in python. what im trying to do is to move my old code into multiprocessing however im facing some errors that i hope anyone could help me out. My code is used to check a few thou…

Fastest possible generation of permutation with defined element values in Python

Trying to generate permutations, could be used with generator or produced List of Lists (but maybe I need a lot of memory?) Looked on the Internet and SO, but couldnt find a version where I define the…

Obtain coordinates of a Polygon / Multi-polygon around a point in python [duplicate]

This question already has answers here:Draw a polygon around point in scattermapbox using python(2 answers)Closed 2 years ago.I am using plotlys scattermapbox to draw a polygon around a point object. I…

PyCuda Error in Execution

This is my pycuda code for rotation.I have installed the latest cuda drivers and I use a nvidia gpu with cuda support.I have also installed the cuda toolkit and pycuda drivers.Still I get this strange …

Python code to Download Specific JSON key value data through REST API calls

I am trying to write a code in python which download only specific key value in the Calls. So the solution might beDownloading the Raw data and later removing the unwanted through regex or 2)Applying s…