Intro
In [1]:
Copied!
import string
import random
import string
import random
In [2]:
Copied!
#testing the function created
#Created on youtube lesson week 10
def generate_random_string(length):
letters = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
#testing the function created
#Created on youtube lesson week 10
def generate_random_string(length):
letters = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
In [3]:
Copied!
generate_random_string(10)
generate_random_string(10)
Out[3]:
"iw2$+PP'de"
In [4]:
Copied!
list(range(10))
list(range(10))
Out[4]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [5]:
Copied!
#improving the function
def generate_random_string(length, upper= False, digits = False, punctuation= False):
letters = string.ascii_lowercase
if upper:
letters += string.ascii_uppercase
if digits:
letters += string.digits
if punctuation:
letters += string.punctuation
#letters = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
#improving the function
def generate_random_string(length, upper= False, digits = False, punctuation= False):
letters = string.ascii_lowercase
if upper:
letters += string.ascii_uppercase
if digits:
letters += string.digits
if punctuation:
letters += string.punctuation
#letters = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
In [6]:
Copied!
generate_random_string(15, upper = True, digits = True, punctuation = True)
generate_random_string(15, upper = True, digits = True, punctuation = True)
Out[6]:
'I5O{[5:]2Z}vqzk'
In [7]:
Copied!
generate_random_string(10) #this does not work beacuse the function is needs to be called
generate_random_string(10) #this does not work beacuse the function is needs to be called
Out[7]:
'bnplkiwsay'
In [8]:
Copied!
#from geosdemo.geosdemo import generate_random_string #geosdemo(folder).geosdemo(file)
#from geosdemo.geosdemo import generate_random_string #geosdemo(folder).geosdemo(file)
In [9]:
Copied!
generate_random_string(5) #here it needs because it is not our funtion from our class
generate_random_string(5) #here it needs because it is not our funtion from our class
Out[9]:
'yewxs'
In [10]:
Copied!
#by implenting the extraction from __init___ we can import normally the function
import geosdemo
#by implenting the extraction from __init___ we can import normally the function
import geosdemo
In [11]:
Copied!
geosdemo.generate_random_string()
geosdemo.generate_random_string()
Out[11]:
'Y_v8dL^uG0'
In [12]:
Copied!
geosdemo.generate_random_string(50)
geosdemo.generate_random_string(50)
Out[12]:
'p($&{/|>/8X3;nS?tT,;k=:DyYZzM%7!toxy2-B4R!2"dD|d1B'
In [13]:
Copied!
geosdemo.generate_lucky_number(5)
geosdemo.generate_lucky_number(5)
Out[13]:
30126
In [ ]:
Copied!
Last update:
2023-05-11