{"id":266,"date":"2013-12-27T12:05:05","date_gmt":"2013-12-27T17:05:05","guid":{"rendered":"http:\/\/www.popmartian.com\/tipsntricks\/?p=266"},"modified":"2014-01-03T15:53:05","modified_gmt":"2014-01-03T20:53:05","slug":"howto-create-a-random-alpha-numeric-string-in-python","status":"publish","type":"post","link":"https:\/\/www.popmartian.com\/tipsntricks\/2013\/12\/27\/howto-create-a-random-alpha-numeric-string-in-python\/","title":{"rendered":"HOWTO: Create a random alpha-numeric string in Python"},"content":{"rendered":"<p>Creating an alpha-numeric string in python is easy, but having a quick function you can call or import as a module is better!<\/p>\n<p>This is very useful for things like password generators.<\/p>\n<p>To see how to do this in Perl, check out this post: <a href=\"http:\/\/www.popmartian.com\/tipsntricks\/2013\/12\/27\/howto-create-a-random-alpha-numeric-string-in-perl\/\" title=\"HOWTO: Create a random alpha-numeric string in Perl\">&#8230;howto-create-a-random-alpha-numeric-string-in-perl<\/a><\/p>\n<p>To see how to do this in PHP, check out this post: <a href=\"http:\/\/www.popmartian.com\/tipsntricks\/2014\/01\/02\/howto-create-a-random-alpha-numeric-string-in-php\/\" title=\"HOWTO: Create a random alpha-numeric string in PHP\">&#8230;howto-create-a-random-alpha-numeric-string-in-php<\/a><\/p>\n<p><code><br \/>\ndef makeID(makeIDchars=6):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;import random, string<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;def join(a,b): return a+b[int(random.randrange(len(b)))]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;makeIDpool = string.letters[0:52] + string.digits<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;x=''<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return ''.join(map((lambda y: join(x, makeIDpool)), range(makeIDchars)))<br \/>\n<\/code><\/p>\n<p>To call it, just set a variable equal to the return value.  You can optionally pass a length to get a longer or shorter string.<\/p>\n<p>Example:<\/p>\n<p>This code sample:<br \/>\n<code><br \/>\nprint makeID()<br \/>\nprint makeID(10)<br \/>\nprint makeID(20)<br \/>\nprint makeID(1)<br \/>\n<\/code><\/p>\n<p>Will print something like this:<br \/>\n<code>oaCCyB<br \/>\nRodUs56H7Z<br \/>\nbGdwsBkVMt27NvGAZTwM<br \/>\nB<\/code><\/p>\n<blockquote><p>Did you find this post useful or have questions or comments?  Please let  me know!<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Creating an alpha-numeric string in python is easy, but having a quick function you can call or import as a module is better! This is very useful for things like password generators. To see how to do this in Perl, &hellip; <a href=\"https:\/\/www.popmartian.com\/tipsntricks\/2013\/12\/27\/howto-create-a-random-alpha-numeric-string-in-python\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,9,59],"tags":[],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-how-to-do-stuff","category-programming","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":15,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":298,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/posts\/266\/revisions\/298"}],"wp:attachment":[{"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.popmartian.com\/tipsntricks\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}