Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence of letters in Python

Tags:

Is there a built-in method / module in Python to generate letters such as the built-in constant LETTERS or letters constant in R?

The R built-in constant works as letters[n] where if n = 1:26 the lower-case letters of the alphabet are produced.

Thanks.

like image 924
John Avatar asked Oct 11 '12 08:10

John


People also ask

How do you create a sequence of alphabets in Python?

To produce a range of letters (characters) in Python, you have to write a custom function that: Takes start and end characters as input. Converts start and end to numbers using ord() function. Generates a range of numbers between start and end.

What is a sequence of characters in Python?

A sequence is just a set of two or more characters and an escape where the sequence begins with a backslash (\\) and other characters in the set follow that backslash.

How do I get a list of letters in Python?

The easiest way to load a list of all the letters of the alphabet is to use the string. ascii_letters , string. ascii_lowercase , and string. ascii_uppercase instances.


1 Answers

It's called string.ascii_lowercase.

If you wanted to pick n many random lower case letters, then:

from string import ascii_lowercase from random import choice  letters = [choice(ascii_lowercase) for _ in range(5)] 

If you wanted it as a string, rather than a list then use str.join:

letters = ''.join([choice(ascii_lowercase) for _ in range(5)]) 
like image 165
Jon Clements Avatar answered Sep 21 '22 18:09

Jon Clements