Python String | ascii_letters

In Python3, ascii_letters is a pre-initialized string used as string constant.
ascii_letters is basically concatenation of ascii_lowercase and ascii_uppercase string constants. Also, the value generated is not locale-dependent, hence, doesn’t change.

Syntax :

string.ascii_letters

Note : Make sure to import string library function inorder to use ascii_letters.



Parameters :

 Doesn't take any parameter, since it's not a function. 

Returns :

 Return all ASCII letters (both lower and upper case)

 
Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# import string library function
import string
  
# Storing the value in variable result
result = string.ascii_letters
  
# Printing the value
print(result)

chevron_right


Output :

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

 

Code #2 :
Given code checks if the string input has only ASCII characters or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing string library function
import string
  
# Function checks if input string
# has only ascii letters or not
def check(value):
    for letter in value:
          
        # If anything other than ascii
        # letter is present, then return
        # False, else return True
        if letter not in string.ascii_letters:
            return False
    return True
  
# Driver Code
input1 = "GeeksForGeeks"
print(input1, "--> ",  check(input1))
  
input2 = "Geeks for Geeks"
print(input2, "--> ", check(input2))
  
input3 = "Geeks_for_geeks"
print(input3, "--> ", check(input3))

chevron_right


Output :

GeeksForGeeks -->  True
Geeks for Geeks -->  False
Geeks_for_geeks -->  False

 
Applications :
The string constant ascii_letters can be used in many practical applications.
Let’s see a code explaining how to use ascii_letters to generate strong random passwords of given size.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing random to generate
# random string sequence
import random
  
# Importing string library function
import string
  
def rand_pass(size):
      
    # Takes random choices from
    # ascii_letters and digits
    generate_pass = ''.join([random.choice(
                        string.ascii_letters + string.digits)
                        for n in range(size)])
                          
    return generate_pass
  
# Driver Code 
password = rand_pass(10)
print(password)
      
    

chevron_right


Output :

oQjI5MOXQ3

Note : Above given code will print random (different) password everytime, for the size provided.
 
Code #2 :
Say if you want to generate random password, but from the set of given string. Let’s see how can we do this using ascii_letters :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing random to generate
# random string sequence
import random
  
# Importing string library function
import string
  
def rand_pass(size, scope = string.ascii_letters + string.digits):
      
    # Takes random choices from ascii_letters and digits
    generate_pass = ''.join([random.choice(scope)
                             for n in range(size)])
                         
    return generate_pass
  
# Driver Code 
password = rand_pass(10, 'Geeks3F0rgeeKs')
print(password)

chevron_right


Output :

kg3g03keG3


My Personal Notes arrow_drop_up

WorKing to be bEst veRsion of MySelf

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.