[{"data":1,"prerenderedAt":1865},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-random-password-list-generator-example":3},{"id":4,"title":5,"body":6,"description":1858,"extension":1859,"meta":1860,"navigation":84,"path":1861,"seo":1862,"stem":1863,"__hash__":1864},"content\u002Fexamples\u002Fpython-random-password-list-generator-example.md","Python Random Password List Generator Example",{"type":7,"value":8,"toc":1826},"minimark",[9,13,17,20,41,44,49,223,226,230,233,244,248,251,272,276,279,397,402,405,413,417,486,490,493,526,529,539,542,588,591,614,621,625,628,636,817,821,845,857,861,870,873,900,907,915,919,922,926,929,946,949,1049,1053,1056,1168,1172,1175,1326,1330,1333,1510,1516,1520,1523,1552,1556,1559,1591,1595,1598,1667,1670,1689,1693,1696,1699,1717,1724,1728,1732,1738,1742,1750,1754,1759,1763,1766,1770,1805,1808,1822],[10,11,5],"h1",{"id":12},"python-random-password-list-generator-example",[14,15,16],"p",{},"This example shows how to build a simple Python script that generates a list of random passwords.",[14,18,19],{},"You will use:",[21,22,23,32,35,38],"ul",{},[24,25,26,27,31],"li",{},"the ",[28,29,30],"code",{},"random"," module",[24,33,34],{},"strings of allowed characters",[24,36,37],{},"loops",[24,39,40],{},"a little user input for customization",[14,42,43],{},"This is a good beginner project because it combines several basic Python ideas in one small program.",[45,46,48],"h2",{"id":47},"quick-example","Quick example",[50,51,56],"pre",{"className":52,"code":53,"language":54,"meta":55,"style":55},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    print(password)\n","python","",[28,57,58,71,79,86,126,131,157,209],{"__ignoreMap":55},[59,60,63,67],"span",{"class":61,"line":62},"line",1,[59,64,66],{"class":65},"sVHd0","import",[59,68,70],{"class":69},"su5hD"," random\n",[59,72,74,76],{"class":61,"line":73},2,[59,75,66],{"class":65},[59,77,78],{"class":69}," string\n",[59,80,82],{"class":61,"line":81},3,[59,83,85],{"emptyLinePlaceholder":84},true,"\n",[59,87,89,92,96,99,103,107,110,112,114,117,119,121,123],{"class":61,"line":88},4,[59,90,91],{"class":69},"characters ",[59,93,95],{"class":94},"smGrS","=",[59,97,98],{"class":69}," string",[59,100,102],{"class":101},"sP7_E",".",[59,104,106],{"class":105},"skxfh","ascii_letters",[59,108,109],{"class":94}," +",[59,111,98],{"class":69},[59,113,102],{"class":101},[59,115,116],{"class":105},"digits",[59,118,109],{"class":94},[59,120,98],{"class":69},[59,122,102],{"class":101},[59,124,125],{"class":105},"punctuation\n",[59,127,129],{"class":61,"line":128},5,[59,130,85],{"emptyLinePlaceholder":84},[59,132,134,137,140,143,147,150,154],{"class":61,"line":133},6,[59,135,136],{"class":65},"for",[59,138,139],{"class":69}," _ ",[59,141,142],{"class":65},"in",[59,144,146],{"class":145},"sptTA"," range",[59,148,149],{"class":101},"(",[59,151,153],{"class":152},"srdBf","5",[59,155,156],{"class":101},"):\n",[59,158,160,163,165,169,171,175,177,179,181,184,186,189,192,195,197,199,201,203,206],{"class":61,"line":159},7,[59,161,162],{"class":69},"    password ",[59,164,95],{"class":94},[59,166,168],{"class":167},"sjJ54"," ''",[59,170,102],{"class":101},[59,172,174],{"class":173},"slqww","join",[59,176,149],{"class":101},[59,178,30],{"class":173},[59,180,102],{"class":101},[59,182,183],{"class":173},"choice",[59,185,149],{"class":101},[59,187,188],{"class":173},"characters",[59,190,191],{"class":101},")",[59,193,194],{"class":65}," for",[59,196,139],{"class":173},[59,198,142],{"class":65},[59,200,146],{"class":145},[59,202,149],{"class":101},[59,204,205],{"class":152},"12",[59,207,208],{"class":101},"))\n",[59,210,212,215,217,220],{"class":61,"line":211},8,[59,213,214],{"class":145},"    print",[59,216,149],{"class":101},[59,218,219],{"class":173},"password",[59,221,222],{"class":101},")\n",[14,224,225],{},"This quick example prints 5 random passwords, each 12 characters long.",[45,227,229],{"id":228},"what-this-example-builds","What this example builds",[14,231,232],{},"This script creates:",[21,234,235,238,241],{},[24,236,237],{},"a program that generates multiple random passwords",[24,239,240],{},"passwords made from letters, digits, and symbols",[24,242,243],{},"an easy way to change password length and how many passwords to create",[45,245,247],{"id":246},"what-you-need-to-understand-first","What you need to understand first",[14,249,250],{},"Before looking at the full code, these ideas help:",[21,252,253,256,266,269],{},[24,254,255],{},"A string can hold all characters that are allowed in the password.",[24,257,258,265],{},[259,260,262],"a",{"href":261},"\u002Fstandard-library\u002Frandom.choice-function-explained",[28,263,264],{},"random.choice()"," picks one item from a sequence at random.",[24,267,268],{},"A loop can repeat the character-picking step to build one full password.",[24,270,271],{},"Another loop can repeat the whole process to generate many passwords.",[45,273,275],{"id":274},"basic-password-list-generator","Basic password list generator",[14,277,278],{},"Here is a simple version:",[50,280,281],{"className":52,"code":53,"language":54,"meta":55,"style":55},[28,282,283,289,295,299,327,331,347,387],{"__ignoreMap":55},[59,284,285,287],{"class":61,"line":62},[59,286,66],{"class":65},[59,288,70],{"class":69},[59,290,291,293],{"class":61,"line":73},[59,292,66],{"class":65},[59,294,78],{"class":69},[59,296,297],{"class":61,"line":81},[59,298,85],{"emptyLinePlaceholder":84},[59,300,301,303,305,307,309,311,313,315,317,319,321,323,325],{"class":61,"line":88},[59,302,91],{"class":69},[59,304,95],{"class":94},[59,306,98],{"class":69},[59,308,102],{"class":101},[59,310,106],{"class":105},[59,312,109],{"class":94},[59,314,98],{"class":69},[59,316,102],{"class":101},[59,318,116],{"class":105},[59,320,109],{"class":94},[59,322,98],{"class":69},[59,324,102],{"class":101},[59,326,125],{"class":105},[59,328,329],{"class":61,"line":128},[59,330,85],{"emptyLinePlaceholder":84},[59,332,333,335,337,339,341,343,345],{"class":61,"line":133},[59,334,136],{"class":65},[59,336,139],{"class":69},[59,338,142],{"class":65},[59,340,146],{"class":145},[59,342,149],{"class":101},[59,344,153],{"class":152},[59,346,156],{"class":101},[59,348,349,351,353,355,357,359,361,363,365,367,369,371,373,375,377,379,381,383,385],{"class":61,"line":159},[59,350,162],{"class":69},[59,352,95],{"class":94},[59,354,168],{"class":167},[59,356,102],{"class":101},[59,358,174],{"class":173},[59,360,149],{"class":101},[59,362,30],{"class":173},[59,364,102],{"class":101},[59,366,183],{"class":173},[59,368,149],{"class":101},[59,370,188],{"class":173},[59,372,191],{"class":101},[59,374,194],{"class":65},[59,376,139],{"class":173},[59,378,142],{"class":65},[59,380,146],{"class":145},[59,382,149],{"class":101},[59,384,205],{"class":152},[59,386,208],{"class":101},[59,388,389,391,393,395],{"class":61,"line":211},[59,390,214],{"class":145},[59,392,149],{"class":101},[59,394,219],{"class":173},[59,396,222],{"class":101},[398,399,401],"h3",{"id":400},"example-output","Example output",[14,403,404],{},"Your output will be different each time, but it may look like this:",[50,406,411],{"className":407,"code":409,"language":410,"meta":55},[408],"language-text","d8@Kp!2Lm#Q1\n7&nZ4^xP0!aB\nm$T9q@W2e*R5\n!L8v#Y1c%N6s\nQ2@fG7&kP!z4\n","text",[28,412,409],{"__ignoreMap":55},[398,414,416],{"id":415},"what-this-code-does","What this code does",[21,418,419,425,431,437,450,467,474,477],{},[24,420,421,424],{},[28,422,423],{},"import random"," loads Python's random tools.",[24,426,427,430],{},[28,428,429],{},"import string"," gives you ready-made character groups.",[24,432,433,436],{},[28,434,435],{},"string.ascii_letters"," adds lowercase and uppercase letters.",[24,438,439,442,443,446,447,102],{},[28,440,441],{},"string.digits"," adds numbers from ",[28,444,445],{},"0"," to ",[28,448,449],{},"9",[24,451,452,455,456,459,460,463,464,102],{},[28,453,454],{},"string.punctuation"," adds symbols like ",[28,457,458],{},"!",", ",[28,461,462],{},"@",", and ",[28,465,466],{},"#",[24,468,469,470,473],{},"The ",[28,471,472],{},"for _ in range(5)"," loop creates 5 passwords.",[24,475,476],{},"The inner part builds a 12-character password by picking one random character at a time.",[24,478,479,485],{},[259,480,482],{"href":481},"\u002Freference\u002Fpython-string-join-method",[28,483,484],{},"''.join()"," combines those characters into one string.",[45,487,489],{"id":488},"how-the-code-works","How the code works",[14,491,492],{},"This line creates the allowed character set:",[50,494,496],{"className":52,"code":495,"language":54,"meta":55,"style":55},"characters = string.ascii_letters + string.digits + string.punctuation\n",[28,497,498],{"__ignoreMap":55},[59,499,500,502,504,506,508,510,512,514,516,518,520,522,524],{"class":61,"line":62},[59,501,91],{"class":69},[59,503,95],{"class":94},[59,505,98],{"class":69},[59,507,102],{"class":101},[59,509,106],{"class":105},[59,511,109],{"class":94},[59,513,98],{"class":69},[59,515,102],{"class":101},[59,517,116],{"class":105},[59,519,109],{"class":94},[59,521,98],{"class":69},[59,523,102],{"class":101},[59,525,125],{"class":105},[14,527,528],{},"It combines:",[21,530,531,534,536],{},[24,532,533],{},"letters",[24,535,116],{},[24,537,538],{},"punctuation symbols",[14,540,541],{},"This line builds one password:",[50,543,545],{"className":52,"code":544,"language":54,"meta":55,"style":55},"password = ''.join(random.choice(characters) for _ in range(12))\n",[28,546,547],{"__ignoreMap":55},[59,548,549,552,554,556,558,560,562,564,566,568,570,572,574,576,578,580,582,584,586],{"class":61,"line":62},[59,550,551],{"class":69},"password ",[59,553,95],{"class":94},[59,555,168],{"class":167},[59,557,102],{"class":101},[59,559,174],{"class":173},[59,561,149],{"class":101},[59,563,30],{"class":173},[59,565,102],{"class":101},[59,567,183],{"class":173},[59,569,149],{"class":101},[59,571,188],{"class":173},[59,573,191],{"class":101},[59,575,194],{"class":65},[59,577,139],{"class":173},[59,579,142],{"class":65},[59,581,146],{"class":145},[59,583,149],{"class":101},[59,585,205],{"class":152},[59,587,208],{"class":101},[14,589,590],{},"Here is what happens:",[21,592,593,599,605,608],{},[24,594,595,598],{},[28,596,597],{},"range(12)"," means \"do this 12 times\"",[24,600,601,604],{},[28,602,603],{},"random.choice(characters)"," picks one random character each time",[24,606,607],{},"the generator expression produces 12 random characters",[24,609,610,613],{},[28,611,612],{},"join()"," combines them into one password string",[14,615,616,617,102],{},"If you want a deeper explanation of random tools, see the ",[259,618,620],{"href":619},"\u002Fstandard-library\u002Fpython-random-module-overview\u002F","Python random module overview",[45,622,624],{"id":623},"let-the-user-control-length-and-count","Let the user control length and count",[14,626,627],{},"You can improve the script by asking the user for:",[21,629,630,633],{},[24,631,632],{},"password length",[24,634,635],{},"number of passwords to generate",[50,637,639],{"className":52,"code":638,"language":54,"meta":55,"style":55},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\n\nlength = int(input(\"Enter password length: \"))\ncount = int(input(\"How many passwords do you want to generate? \"))\n\nfor _ in range(count):\n    password = ''.join(random.choice(characters) for _ in range(length))\n    print(password)\n",[28,640,641,647,653,657,685,689,718,742,746,764,806],{"__ignoreMap":55},[59,642,643,645],{"class":61,"line":62},[59,644,66],{"class":65},[59,646,70],{"class":69},[59,648,649,651],{"class":61,"line":73},[59,650,66],{"class":65},[59,652,78],{"class":69},[59,654,655],{"class":61,"line":81},[59,656,85],{"emptyLinePlaceholder":84},[59,658,659,661,663,665,667,669,671,673,675,677,679,681,683],{"class":61,"line":88},[59,660,91],{"class":69},[59,662,95],{"class":94},[59,664,98],{"class":69},[59,666,102],{"class":101},[59,668,106],{"class":105},[59,670,109],{"class":94},[59,672,98],{"class":69},[59,674,102],{"class":101},[59,676,116],{"class":105},[59,678,109],{"class":94},[59,680,98],{"class":69},[59,682,102],{"class":101},[59,684,125],{"class":105},[59,686,687],{"class":61,"line":128},[59,688,85],{"emptyLinePlaceholder":84},[59,690,691,694,696,700,702,705,707,710,714,716],{"class":61,"line":133},[59,692,693],{"class":69},"length ",[59,695,95],{"class":94},[59,697,699],{"class":698},"sZMiF"," int",[59,701,149],{"class":101},[59,703,704],{"class":145},"input",[59,706,149],{"class":101},[59,708,709],{"class":167},"\"",[59,711,713],{"class":712},"s_sjI","Enter password length: ",[59,715,709],{"class":167},[59,717,208],{"class":101},[59,719,720,723,725,727,729,731,733,735,738,740],{"class":61,"line":159},[59,721,722],{"class":69},"count ",[59,724,95],{"class":94},[59,726,699],{"class":698},[59,728,149],{"class":101},[59,730,704],{"class":145},[59,732,149],{"class":101},[59,734,709],{"class":167},[59,736,737],{"class":712},"How many passwords do you want to generate? ",[59,739,709],{"class":167},[59,741,208],{"class":101},[59,743,744],{"class":61,"line":211},[59,745,85],{"emptyLinePlaceholder":84},[59,747,749,751,753,755,757,759,762],{"class":61,"line":748},9,[59,750,136],{"class":65},[59,752,139],{"class":69},[59,754,142],{"class":65},[59,756,146],{"class":145},[59,758,149],{"class":101},[59,760,761],{"class":173},"count",[59,763,156],{"class":101},[59,765,767,769,771,773,775,777,779,781,783,785,787,789,791,793,795,797,799,801,804],{"class":61,"line":766},10,[59,768,162],{"class":69},[59,770,95],{"class":94},[59,772,168],{"class":167},[59,774,102],{"class":101},[59,776,174],{"class":173},[59,778,149],{"class":101},[59,780,30],{"class":173},[59,782,102],{"class":101},[59,784,183],{"class":173},[59,786,149],{"class":101},[59,788,188],{"class":173},[59,790,191],{"class":101},[59,792,194],{"class":65},[59,794,139],{"class":173},[59,796,142],{"class":65},[59,798,146],{"class":145},[59,800,149],{"class":101},[59,802,803],{"class":173},"length",[59,805,208],{"class":101},[59,807,809,811,813,815],{"class":61,"line":808},11,[59,810,214],{"class":145},[59,812,149],{"class":101},[59,814,219],{"class":173},[59,816,222],{"class":101},[398,818,820],{"id":819},"how-this-version-works","How this version works",[21,822,823,829,835,840],{},[24,824,825,828],{},[28,826,827],{},"input()"," reads text typed by the user",[24,830,831,834],{},[28,832,833],{},"int()"," converts that text to an integer",[24,836,837,839],{},[28,838,803],{}," controls how many characters are in each password",[24,841,842,844],{},[28,843,761],{}," controls how many passwords are generated",[14,846,847,848,852,853,102],{},"If you are new to this, read ",[259,849,851],{"href":850},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python\u002F","how to get user input in Python"," and ",[259,854,856],{"href":855},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert a string to an int in Python",[398,858,860],{"id":859},"important-note-about-errors","Important note about errors",[14,862,863,864,866,867,102],{},"If the user types something that is not a whole number, ",[28,865,833],{}," will fail with a ",[28,868,869],{},"ValueError",[14,871,872],{},"For example:",[50,874,876],{"className":52,"code":875,"language":54,"meta":55,"style":55},"length = int(input(\"Enter password length: \"))\n",[28,877,878],{"__ignoreMap":55},[59,879,880,882,884,886,888,890,892,894,896,898],{"class":61,"line":62},[59,881,693],{"class":69},[59,883,95],{"class":94},[59,885,699],{"class":698},[59,887,149],{"class":101},[59,889,704],{"class":145},[59,891,149],{"class":101},[59,893,709],{"class":167},[59,895,713],{"class":712},[59,897,709],{"class":167},[59,899,208],{"class":101},[14,901,902,903,906],{},"If the user enters ",[28,904,905],{},"abc",", Python raises an error.",[14,908,909,910,914],{},"See ",[259,911,913],{"href":912},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","how to fix ValueError: invalid literal for int() with base 10"," if that happens.",[45,916,918],{"id":917},"simple-improvements","Simple improvements",[14,920,921],{},"Here are a few easy ways to change the program.",[398,923,925],{"id":924},"avoid-hard-to-read-characters","Avoid hard-to-read characters",[14,927,928],{},"Some characters are easy to confuse, such as:",[21,930,931,938],{},[24,932,933,852,936],{},[28,934,935],{},"O",[28,937,445],{},[24,939,940,852,943],{},[28,941,942],{},"l",[28,944,945],{},"1",[14,947,948],{},"You can build your own character set:",[50,950,952],{"className":52,"code":951,"language":54,"meta":55,"style":55},"import random\n\ncharacters = \"abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789\"\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    print(password)\n",[28,953,954,960,964,979,983,999,1039],{"__ignoreMap":55},[59,955,956,958],{"class":61,"line":62},[59,957,66],{"class":65},[59,959,70],{"class":69},[59,961,962],{"class":61,"line":73},[59,963,85],{"emptyLinePlaceholder":84},[59,965,966,968,970,973,976],{"class":61,"line":81},[59,967,91],{"class":69},[59,969,95],{"class":94},[59,971,972],{"class":167}," \"",[59,974,975],{"class":712},"abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789",[59,977,978],{"class":167},"\"\n",[59,980,981],{"class":61,"line":88},[59,982,85],{"emptyLinePlaceholder":84},[59,984,985,987,989,991,993,995,997],{"class":61,"line":128},[59,986,136],{"class":65},[59,988,139],{"class":69},[59,990,142],{"class":65},[59,992,146],{"class":145},[59,994,149],{"class":101},[59,996,153],{"class":152},[59,998,156],{"class":101},[59,1000,1001,1003,1005,1007,1009,1011,1013,1015,1017,1019,1021,1023,1025,1027,1029,1031,1033,1035,1037],{"class":61,"line":133},[59,1002,162],{"class":69},[59,1004,95],{"class":94},[59,1006,168],{"class":167},[59,1008,102],{"class":101},[59,1010,174],{"class":173},[59,1012,149],{"class":101},[59,1014,30],{"class":173},[59,1016,102],{"class":101},[59,1018,183],{"class":173},[59,1020,149],{"class":101},[59,1022,188],{"class":173},[59,1024,191],{"class":101},[59,1026,194],{"class":65},[59,1028,139],{"class":173},[59,1030,142],{"class":65},[59,1032,146],{"class":145},[59,1034,149],{"class":101},[59,1036,205],{"class":152},[59,1038,208],{"class":101},[59,1040,1041,1043,1045,1047],{"class":61,"line":159},[59,1042,214],{"class":145},[59,1044,149],{"class":101},[59,1046,219],{"class":173},[59,1048,222],{"class":101},[398,1050,1052],{"id":1051},"generate-passwords-without-symbols","Generate passwords without symbols",[14,1054,1055],{},"If you only want letters and numbers:",[50,1057,1059],{"className":52,"code":1058,"language":54,"meta":55,"style":55},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    print(password)\n",[28,1060,1061,1067,1073,1077,1098,1102,1118,1158],{"__ignoreMap":55},[59,1062,1063,1065],{"class":61,"line":62},[59,1064,66],{"class":65},[59,1066,70],{"class":69},[59,1068,1069,1071],{"class":61,"line":73},[59,1070,66],{"class":65},[59,1072,78],{"class":69},[59,1074,1075],{"class":61,"line":81},[59,1076,85],{"emptyLinePlaceholder":84},[59,1078,1079,1081,1083,1085,1087,1089,1091,1093,1095],{"class":61,"line":88},[59,1080,91],{"class":69},[59,1082,95],{"class":94},[59,1084,98],{"class":69},[59,1086,102],{"class":101},[59,1088,106],{"class":105},[59,1090,109],{"class":94},[59,1092,98],{"class":69},[59,1094,102],{"class":101},[59,1096,1097],{"class":105},"digits\n",[59,1099,1100],{"class":61,"line":128},[59,1101,85],{"emptyLinePlaceholder":84},[59,1103,1104,1106,1108,1110,1112,1114,1116],{"class":61,"line":133},[59,1105,136],{"class":65},[59,1107,139],{"class":69},[59,1109,142],{"class":65},[59,1111,146],{"class":145},[59,1113,149],{"class":101},[59,1115,153],{"class":152},[59,1117,156],{"class":101},[59,1119,1120,1122,1124,1126,1128,1130,1132,1134,1136,1138,1140,1142,1144,1146,1148,1150,1152,1154,1156],{"class":61,"line":159},[59,1121,162],{"class":69},[59,1123,95],{"class":94},[59,1125,168],{"class":167},[59,1127,102],{"class":101},[59,1129,174],{"class":173},[59,1131,149],{"class":101},[59,1133,30],{"class":173},[59,1135,102],{"class":101},[59,1137,183],{"class":173},[59,1139,149],{"class":101},[59,1141,188],{"class":173},[59,1143,191],{"class":101},[59,1145,194],{"class":65},[59,1147,139],{"class":173},[59,1149,142],{"class":65},[59,1151,146],{"class":145},[59,1153,149],{"class":101},[59,1155,205],{"class":152},[59,1157,208],{"class":101},[59,1159,1160,1162,1164,1166],{"class":61,"line":211},[59,1161,214],{"class":145},[59,1163,149],{"class":101},[59,1165,219],{"class":173},[59,1167,222],{"class":101},[398,1169,1171],{"id":1170},"store-passwords-in-a-list","Store passwords in a list",[14,1173,1174],{},"Instead of printing passwords right away, you can save them in a list:",[50,1176,1178],{"className":52,"code":1177,"language":54,"meta":55,"style":55},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\npasswords = []\n\nfor _ in range(5):\n    password = ''.join(random.choice(characters) for _ in range(12))\n    passwords.append(password)\n\nprint(passwords)\n",[28,1179,1180,1186,1192,1196,1224,1234,1238,1254,1294,1310,1314],{"__ignoreMap":55},[59,1181,1182,1184],{"class":61,"line":62},[59,1183,66],{"class":65},[59,1185,70],{"class":69},[59,1187,1188,1190],{"class":61,"line":73},[59,1189,66],{"class":65},[59,1191,78],{"class":69},[59,1193,1194],{"class":61,"line":81},[59,1195,85],{"emptyLinePlaceholder":84},[59,1197,1198,1200,1202,1204,1206,1208,1210,1212,1214,1216,1218,1220,1222],{"class":61,"line":88},[59,1199,91],{"class":69},[59,1201,95],{"class":94},[59,1203,98],{"class":69},[59,1205,102],{"class":101},[59,1207,106],{"class":105},[59,1209,109],{"class":94},[59,1211,98],{"class":69},[59,1213,102],{"class":101},[59,1215,116],{"class":105},[59,1217,109],{"class":94},[59,1219,98],{"class":69},[59,1221,102],{"class":101},[59,1223,125],{"class":105},[59,1225,1226,1229,1231],{"class":61,"line":128},[59,1227,1228],{"class":69},"passwords ",[59,1230,95],{"class":94},[59,1232,1233],{"class":101}," []\n",[59,1235,1236],{"class":61,"line":133},[59,1237,85],{"emptyLinePlaceholder":84},[59,1239,1240,1242,1244,1246,1248,1250,1252],{"class":61,"line":159},[59,1241,136],{"class":65},[59,1243,139],{"class":69},[59,1245,142],{"class":65},[59,1247,146],{"class":145},[59,1249,149],{"class":101},[59,1251,153],{"class":152},[59,1253,156],{"class":101},[59,1255,1256,1258,1260,1262,1264,1266,1268,1270,1272,1274,1276,1278,1280,1282,1284,1286,1288,1290,1292],{"class":61,"line":211},[59,1257,162],{"class":69},[59,1259,95],{"class":94},[59,1261,168],{"class":167},[59,1263,102],{"class":101},[59,1265,174],{"class":173},[59,1267,149],{"class":101},[59,1269,30],{"class":173},[59,1271,102],{"class":101},[59,1273,183],{"class":173},[59,1275,149],{"class":101},[59,1277,188],{"class":173},[59,1279,191],{"class":101},[59,1281,194],{"class":65},[59,1283,139],{"class":173},[59,1285,142],{"class":65},[59,1287,146],{"class":145},[59,1289,149],{"class":101},[59,1291,205],{"class":152},[59,1293,208],{"class":101},[59,1295,1296,1299,1301,1304,1306,1308],{"class":61,"line":748},[59,1297,1298],{"class":69},"    passwords",[59,1300,102],{"class":101},[59,1302,1303],{"class":173},"append",[59,1305,149],{"class":101},[59,1307,219],{"class":173},[59,1309,222],{"class":101},[59,1311,1312],{"class":61,"line":766},[59,1313,85],{"emptyLinePlaceholder":84},[59,1315,1316,1319,1321,1324],{"class":61,"line":808},[59,1317,1318],{"class":145},"print",[59,1320,149],{"class":101},[59,1322,1323],{"class":173},"passwords",[59,1325,222],{"class":101},[398,1327,1329],{"id":1328},"write-passwords-to-a-text-file","Write passwords to a text file",[14,1331,1332],{},"You can also save the generated passwords to a file:",[50,1334,1336],{"className":52,"code":1335,"language":54,"meta":55,"style":55},"import random\nimport string\n\ncharacters = string.ascii_letters + string.digits + string.punctuation\n\nwith open(\"passwords.txt\", \"w\") as file:\n    for _ in range(5):\n        password = ''.join(random.choice(characters) for _ in range(12))\n        file.write(password + \"\\n\")\n",[28,1337,1338,1344,1350,1354,1382,1386,1425,1442,1483],{"__ignoreMap":55},[59,1339,1340,1342],{"class":61,"line":62},[59,1341,66],{"class":65},[59,1343,70],{"class":69},[59,1345,1346,1348],{"class":61,"line":73},[59,1347,66],{"class":65},[59,1349,78],{"class":69},[59,1351,1352],{"class":61,"line":81},[59,1353,85],{"emptyLinePlaceholder":84},[59,1355,1356,1358,1360,1362,1364,1366,1368,1370,1372,1374,1376,1378,1380],{"class":61,"line":88},[59,1357,91],{"class":69},[59,1359,95],{"class":94},[59,1361,98],{"class":69},[59,1363,102],{"class":101},[59,1365,106],{"class":105},[59,1367,109],{"class":94},[59,1369,98],{"class":69},[59,1371,102],{"class":101},[59,1373,116],{"class":105},[59,1375,109],{"class":94},[59,1377,98],{"class":69},[59,1379,102],{"class":101},[59,1381,125],{"class":105},[59,1383,1384],{"class":61,"line":128},[59,1385,85],{"emptyLinePlaceholder":84},[59,1387,1388,1391,1394,1396,1398,1401,1403,1406,1408,1411,1413,1415,1418,1422],{"class":61,"line":133},[59,1389,1390],{"class":65},"with",[59,1392,1393],{"class":145}," open",[59,1395,149],{"class":101},[59,1397,709],{"class":167},[59,1399,1400],{"class":712},"passwords.txt",[59,1402,709],{"class":167},[59,1404,1405],{"class":101},",",[59,1407,972],{"class":167},[59,1409,1410],{"class":712},"w",[59,1412,709],{"class":167},[59,1414,191],{"class":101},[59,1416,1417],{"class":65}," as",[59,1419,1421],{"class":1420},"sMMDD"," file",[59,1423,1424],{"class":101},":\n",[59,1426,1427,1430,1432,1434,1436,1438,1440],{"class":61,"line":159},[59,1428,1429],{"class":65},"    for",[59,1431,139],{"class":69},[59,1433,142],{"class":65},[59,1435,146],{"class":145},[59,1437,149],{"class":101},[59,1439,153],{"class":152},[59,1441,156],{"class":101},[59,1443,1444,1447,1449,1451,1453,1455,1457,1459,1461,1463,1465,1467,1469,1471,1473,1475,1477,1479,1481],{"class":61,"line":211},[59,1445,1446],{"class":69},"        password ",[59,1448,95],{"class":94},[59,1450,168],{"class":167},[59,1452,102],{"class":101},[59,1454,174],{"class":173},[59,1456,149],{"class":101},[59,1458,30],{"class":173},[59,1460,102],{"class":101},[59,1462,183],{"class":173},[59,1464,149],{"class":101},[59,1466,188],{"class":173},[59,1468,191],{"class":101},[59,1470,194],{"class":65},[59,1472,139],{"class":173},[59,1474,142],{"class":65},[59,1476,146],{"class":145},[59,1478,149],{"class":101},[59,1480,205],{"class":152},[59,1482,208],{"class":101},[59,1484,1485,1488,1490,1493,1495,1497,1500,1502,1506,1508],{"class":61,"line":748},[59,1486,1487],{"class":1420},"        file",[59,1489,102],{"class":101},[59,1491,1492],{"class":173},"write",[59,1494,149],{"class":101},[59,1496,551],{"class":173},[59,1498,1499],{"class":94},"+",[59,1501,972],{"class":167},[59,1503,1505],{"class":1504},"s_hVV","\\n",[59,1507,709],{"class":167},[59,1509,222],{"class":101},[14,1511,1512,1513,1515],{},"This creates a file named ",[28,1514,1400],{}," and writes one password per line.",[45,1517,1519],{"id":1518},"beginner-mistakes-to-watch-for","Beginner mistakes to watch for",[14,1521,1522],{},"Common problems in this example include:",[21,1524,1525,1534,1540,1546],{},[24,1526,1527,1528,1530,1531],{},"forgetting to import ",[28,1529,30],{}," or ",[28,1532,1533],{},"string",[24,1535,1536,1537,1539],{},"using ",[28,1538,827],{}," values as strings instead of integers",[24,1541,1536,1542,1545],{},[28,1543,1544],{},"random.sample()"," when repeated characters should be allowed",[24,1547,1548,1549,1551],{},"expecting ",[28,1550,264],{}," to create security-grade passwords",[398,1553,1555],{"id":1554},"common-errors","Common errors",[14,1557,1558],{},"You may run into:",[21,1560,1561,1572,1579,1588],{},[24,1562,1563,1566,1567,1530,1569,1571],{},[28,1564,1565],{},"NameError"," because ",[28,1568,30],{},[28,1570,1533],{}," was not imported",[24,1573,1574,1576,1577],{},[28,1575,869],{}," when converting user input with ",[28,1578,833],{},[24,1580,1581,1584,1585],{},[28,1582,1583],{},"TypeError"," from using a string number directly in ",[28,1586,1587],{},"range()",[24,1589,1590],{},"confusion about why some passwords contain repeated characters",[398,1592,1594],{"id":1593},"useful-debugging-checks","Useful debugging checks",[14,1596,1597],{},"If something is not working, print values to inspect them:",[50,1599,1601],{"className":52,"code":1600,"language":54,"meta":55,"style":55},"print(characters)\nprint(len(characters))\nprint(password)\nprint(type(length))\nprint(type(count))\n",[28,1602,1603,1613,1628,1638,1653],{"__ignoreMap":55},[59,1604,1605,1607,1609,1611],{"class":61,"line":62},[59,1606,1318],{"class":145},[59,1608,149],{"class":101},[59,1610,188],{"class":173},[59,1612,222],{"class":101},[59,1614,1615,1617,1619,1622,1624,1626],{"class":61,"line":73},[59,1616,1318],{"class":145},[59,1618,149],{"class":101},[59,1620,1621],{"class":145},"len",[59,1623,149],{"class":101},[59,1625,188],{"class":173},[59,1627,208],{"class":101},[59,1629,1630,1632,1634,1636],{"class":61,"line":81},[59,1631,1318],{"class":145},[59,1633,149],{"class":101},[59,1635,219],{"class":173},[59,1637,222],{"class":101},[59,1639,1640,1642,1644,1647,1649,1651],{"class":61,"line":88},[59,1641,1318],{"class":145},[59,1643,149],{"class":101},[59,1645,1646],{"class":698},"type",[59,1648,149],{"class":101},[59,1650,803],{"class":173},[59,1652,208],{"class":101},[59,1654,1655,1657,1659,1661,1663,1665],{"class":61,"line":128},[59,1656,1318],{"class":145},[59,1658,149],{"class":101},[59,1660,1646],{"class":698},[59,1662,149],{"class":101},[59,1664,761],{"class":173},[59,1666,208],{"class":101},[14,1668,1669],{},"These checks help you confirm:",[21,1671,1672,1675,1678,1681],{},[24,1673,1674],{},"what characters are available",[24,1676,1677],{},"how many characters are in the set",[24,1679,1680],{},"what the generated password looks like",[24,1682,1683,1684,852,1686,1688],{},"whether ",[28,1685,803],{},[28,1687,761],{}," are really integers",[45,1690,1692],{"id":1691},"when-not-to-use-this-approach","When not to use this approach",[14,1694,1695],{},"This example is good for learning, but it is not the best choice for real security.",[14,1697,1698],{},"Keep these points in mind:",[21,1700,1701,1707,1714],{},[24,1702,1703,1704,1706],{},"Do not use the ",[28,1705,30],{}," module for high-security passwords.",[24,1708,1709,1710,1713],{},"For stronger password generation, use the ",[28,1711,1712],{},"secrets"," module.",[24,1715,1716],{},"This example is mainly for practicing loops, strings, and randomness.",[14,1718,1719,1720,102],{},"If you want a simpler version that focuses on creating one password, see ",[259,1721,1723],{"href":1722},"\u002Fexamples\u002Fpython-password-generator-example\u002F","Python password generator example",[45,1725,1727],{"id":1726},"faq","FAQ",[398,1729,1731],{"id":1730},"why-do-some-generated-passwords-repeat-characters","Why do some generated passwords repeat characters?",[14,1733,1734,1735,1737],{},"Because ",[28,1736,264],{}," can pick the same character more than once. That is normal for this approach.",[398,1739,1741],{"id":1740},"can-i-generate-passwords-without-symbols","Can I generate passwords without symbols?",[14,1743,1744,1745,852,1747,1749],{},"Yes. Use only ",[28,1746,435],{},[28,1748,441],{}," in the character set.",[398,1751,1753],{"id":1752},"is-the-random-module-safe-for-real-password-security","Is the random module safe for real password security?",[14,1755,1756,1757,1713],{},"No. It is fine for learning, but real password tools should use the ",[28,1758,1712],{},[398,1760,1762],{"id":1761},"how-do-i-save-the-passwords-to-a-file","How do I save the passwords to a file?",[14,1764,1765],{},"Store them in a list or write each one with a file opened in write mode.",[45,1767,1769],{"id":1768},"see-also","See also",[21,1771,1772,1776,1781,1786,1791,1796,1801],{},[24,1773,1774],{},[259,1775,620],{"href":619},[24,1777,1778],{},[259,1779,1780],{"href":261},"random.choice() function explained",[24,1782,1783],{},[259,1784,1785],{"href":481},"Python string join() method",[24,1787,1788],{},[259,1789,1790],{"href":850},"How to get user input in Python",[24,1792,1793],{},[259,1794,1795],{"href":855},"How to convert string to int in Python",[24,1797,1798],{},[259,1799,1800],{"href":912},"Fix ValueError: invalid literal for int() with base 10",[24,1802,1803],{},[259,1804,1723],{"href":1722},[14,1806,1807],{},"Try improving this program next by:",[21,1809,1810,1813,1816],{},[24,1811,1812],{},"taking user input safely",[24,1814,1815],{},"saving results to a file",[24,1817,1818,1819,1821],{},"rebuilding it with the ",[28,1820,1712],{}," module for stronger password generation",[1823,1824,1825],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":55,"searchDepth":73,"depth":73,"links":1827},[1828,1829,1830,1831,1835,1836,1840,1846,1850,1851,1857],{"id":47,"depth":73,"text":48},{"id":228,"depth":73,"text":229},{"id":246,"depth":73,"text":247},{"id":274,"depth":73,"text":275,"children":1832},[1833,1834],{"id":400,"depth":81,"text":401},{"id":415,"depth":81,"text":416},{"id":488,"depth":73,"text":489},{"id":623,"depth":73,"text":624,"children":1837},[1838,1839],{"id":819,"depth":81,"text":820},{"id":859,"depth":81,"text":860},{"id":917,"depth":73,"text":918,"children":1841},[1842,1843,1844,1845],{"id":924,"depth":81,"text":925},{"id":1051,"depth":81,"text":1052},{"id":1170,"depth":81,"text":1171},{"id":1328,"depth":81,"text":1329},{"id":1518,"depth":73,"text":1519,"children":1847},[1848,1849],{"id":1554,"depth":81,"text":1555},{"id":1593,"depth":81,"text":1594},{"id":1691,"depth":73,"text":1692},{"id":1726,"depth":73,"text":1727,"children":1852},[1853,1854,1855,1856],{"id":1730,"depth":81,"text":1731},{"id":1740,"depth":81,"text":1741},{"id":1752,"depth":81,"text":1753},{"id":1761,"depth":81,"text":1762},{"id":1768,"depth":73,"text":1769},"Master python random password list generator example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-random-password-list-generator-example",{"title":5,"description":1858},"examples\u002Fpython-random-password-list-generator-example","HmwLVsaFPCh_4qQPzUQzyqAy8T-zpDAa0RFJO1U-Sgk",1777585477725]