{"id":29927,"date":"2023-01-12T00:43:09","date_gmt":"2023-01-11T19:13:09","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/"},"modified":"2023-01-12T00:43:09","modified_gmt":"2023-01-11T19:13:09","slug":"solved-python-trying-to-make-a-random-string-generator","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/","title":{"rendered":"[Solved] Python &#8211; Trying to make a Random String Generator"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-41763196\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"41763196\" data-parentid=\"41762506\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Your code is mostly unsalvageable but I&#8217;ll go through it and explain all the issues I&#8217;ve encountered with it. You also didn&#8217;t indent your code properly in your question so I&#8217;ve made some assumptions on that front.<\/p>\n<h2>Addressing Code Issues<\/h2>\n<p><strong>Alphabet generation<\/strong><\/p>\n<pre><code>Alphabet = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\"]`\nAlphabet2 = [\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\",\"K\",\"L\",\"M\",\"N\",\"O\",\"P\",\"Q\",\"R\",\"S\",\"T\",\"U\",\"V\",\"W\",\"X\",\"Y\",\"Z\"]`\n<\/code><\/pre>\n<p>These could be more succinctly expressed as:<\/p>\n<pre><code>lowercase_letters = list(string.ascii_lowercase)\nuppercase_letters = list(string.ascii_uppercase)\n<\/code><\/pre>\n<p><strong>Iteration logic<\/strong><\/p>\n<p>Your current implementation <em>will not iterate at all<\/em> because you assign <code>i = stringsize+1<\/code> and then create a while loop with the condition <code>i &lt; stringsize+1<\/code> &#8211; <em>this will never be true when the condition is first evaluated<\/em>.<\/p>\n<p>The correct and Pythonic approach would be to use a for loop like this:<\/p>\n<pre><code>for i in range(stringsize):\n    ...\n<\/code><\/pre>\n<p><strong>String concatenation<\/strong><\/p>\n<p>Strings in Python are technically lists but it&#8217;s not really very pleasant to construct strings by appending individual characters to a list.<\/p>\n<p>One approach would be to set <code>StringGen = ''<\/code> and then add characters to it using <code>StringGen += c<\/code> within the for loop. However, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/waymoot.org\/home\/python_string\/\">this isn&#8217;t efficient<\/a>. I&#8217;ll provide a solution at the bottom of this post to demonstrate an implementation that does not involve concatenation within loops.<\/p>\n<p><strong>Misusing integers for conditional logic<\/strong><\/p>\n<p>The code:<\/p>\n<pre><code>MajorMin = randint(1,2)\nif MajorMin == 1:\n    ...\nif MajorMin == 2:\n    ...\n<\/code><\/pre>\n<p>Could be made far clearer using this equivalent logic:<\/p>\n<pre><code>use_uppercase_letter = random.choice([True, False])\nif use_uppercase_letter:\n    ...\nelse:\n    ...\n<\/code><\/pre>\n<h2>Alternative Implementations<\/h2>\n<p><strong>Refined variation of your approach<\/strong><\/p>\n<p>Here&#8217;s a different implementation of <code>randomstr<\/code> that builds on the points here:<\/p>\n<pre><code>import string\nimport random\n\n\ndef randomstr(stringsize):\n    lowercase_letters = list(string.ascii_lowercase)\n    uppercase_letters = list(string.ascii_uppercase)\n\n    def generate_letters(n):\n        for i in range(n):\n            use_uppercase_letter = random.choice([True, False])\n            if use_uppercase_letter:\n                yield random.choice(lowercase_letters)\n            else:\n                yield random.choice(uppercase_letters)\n\n    return ''.join(c for c in generate_letters(stringsize))\n\n\nprint(randomstr(10))\n<\/code><\/pre>\n<p><strong>My best crack at it<\/strong><\/p>\n<p>This is a much more concise implementation that I&#8217;ll offer in case you want it, but it deviates a lot from your original approach.<\/p>\n<pre><code>import string\nimport random\n\n\ndef randomstr(stringsize):\n    letters = list(string.ascii_lowercase + string.ascii_uppercase)\n    return ''.join(random.choice(letters) for _ in range(stringsize))\n\n\nprint(randomstr(10))\n<\/code><\/pre>\n<p><strong>Example runs<\/strong><\/p>\n<p>These are the examples of the outputs you get with either of the implementations above.<\/p>\n<pre class=\"lang-none prettyprint-override\"><code>MYXPupqiRG\nELNMPktrbe\nZnYBjlIxNQ\n<\/code><\/pre>\n<hr>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Python &#8211; Trying to make a Random String Generator <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Your code is mostly unsalvageable but I&#8217;ll go through it and explain all the issues I&#8217;ve encountered with it. You also didn&#8217;t indent your code properly in your question so I&#8217;ve made some assumptions on that front. Addressing Code Issues Alphabet generation Alphabet = [&#8220;a&#8221;,&#8221;b&#8221;,&#8221;c&#8221;,&#8221;d&#8221;,&#8221;e&#8221;,&#8221;f&#8221;,&#8221;g&#8221;,&#8221;h&#8221;,&#8221;i&#8221;,&#8221;j&#8221;,&#8221;k&#8221;,&#8221;l&#8221;,&#8221;m&#8221;,&#8221;n&#8221;,&#8221;o&#8221;,&#8221;p&#8221;,&#8221;q&#8221;,&#8221;r&#8221;,&#8221;s&#8221;,&#8221;t&#8221;,&#8221;u&#8221;,&#8221;v&#8221;,&#8221;w&#8221;,&#8221;x&#8221;,&#8221;y&#8221;,&#8221;z&#8221;]` Alphabet2 = [&#8220;A&#8221;,&#8221;B&#8221;,&#8221;C&#8221;,&#8221;D&#8221;,&#8221;E&#8221;,&#8221;F&#8221;,&#8221;G&#8221;,&#8221;H&#8221;,&#8221;I&#8221;,&#8221;J&#8221;,&#8221;K&#8221;,&#8221;L&#8221;,&#8221;M&#8221;,&#8221;N&#8221;,&#8221;O&#8221;,&#8221;P&#8221;,&#8221;Q&#8221;,&#8221;R&#8221;,&#8221;S&#8221;,&#8221;T&#8221;,&#8221;U&#8221;,&#8221;V&#8221;,&#8221;W&#8221;,&#8221;X&#8221;,&#8221;Y&#8221;,&#8221;Z&#8221;]` These could be more succinctly expressed &#8230; <a title=\"[Solved] Python &#8211; Trying to make a Random String Generator\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\" aria-label=\"More on [Solved] Python &#8211; Trying to make a Random String Generator\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[349],"class_list":["post-29927","post","type-post","status-publish","format-standard","hentry","category-solved","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Python - Trying to make a Random String Generator - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Python - Trying to make a Random String Generator - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Your code is mostly unsalvageable but I&#8217;ll go through it and explain all the issues I&#8217;ve encountered with it. You also didn&#8217;t indent your code properly in your question so I&#8217;ve made some assumptions on that front. Addressing Code Issues Alphabet generation Alphabet = [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot;,&quot;j&quot;,&quot;k&quot;,&quot;l&quot;,&quot;m&quot;,&quot;n&quot;,&quot;o&quot;,&quot;p&quot;,&quot;q&quot;,&quot;r&quot;,&quot;s&quot;,&quot;t&quot;,&quot;u&quot;,&quot;v&quot;,&quot;w&quot;,&quot;x&quot;,&quot;y&quot;,&quot;z&quot;]` Alphabet2 = [&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;,&quot;F&quot;,&quot;G&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;L&quot;,&quot;M&quot;,&quot;N&quot;,&quot;O&quot;,&quot;P&quot;,&quot;Q&quot;,&quot;R&quot;,&quot;S&quot;,&quot;T&quot;,&quot;U&quot;,&quot;V&quot;,&quot;W&quot;,&quot;X&quot;,&quot;Y&quot;,&quot;Z&quot;]` These could be more succinctly expressed ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-11T19:13:09+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Python &#8211; Trying to make a Random String Generator\",\"datePublished\":\"2023-01-11T19:13:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\"},\"wordCount\":274,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"python\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\",\"name\":\"[Solved] Python - Trying to make a Random String Generator - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-11T19:13:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Python &#8211; Trying to make a Random String Generator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Python - Trying to make a Random String Generator - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Python - Trying to make a Random String Generator - JassWeb","og_description":"[ad_1] Your code is mostly unsalvageable but I&#8217;ll go through it and explain all the issues I&#8217;ve encountered with it. You also didn&#8217;t indent your code properly in your question so I&#8217;ve made some assumptions on that front. Addressing Code Issues Alphabet generation Alphabet = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\"n\",\"o\",\"p\",\"q\",\"r\",\"s\",\"t\",\"u\",\"v\",\"w\",\"x\",\"y\",\"z\"]` Alphabet2 = [\"A\",\"B\",\"C\",\"D\",\"E\",\"F\",\"G\",\"H\",\"I\",\"J\",\"K\",\"L\",\"M\",\"N\",\"O\",\"P\",\"Q\",\"R\",\"S\",\"T\",\"U\",\"V\",\"W\",\"X\",\"Y\",\"Z\"]` These could be more succinctly expressed ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/","og_site_name":"JassWeb","article_published_time":"2023-01-11T19:13:09+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Python &#8211; Trying to make a Random String Generator","datePublished":"2023-01-11T19:13:09+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/"},"wordCount":274,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["python"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/","url":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/","name":"[Solved] Python - Trying to make a Random String Generator - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-11T19:13:09+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-python-trying-to-make-a-random-string-generator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Python &#8211; Trying to make a Random String Generator"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=29927"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29927\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=29927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=29927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=29927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}