Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 1003 Bytes

2019-02-14-auto-generation-of-URL-strings.md

File metadata and controls

25 lines (19 loc) · 1003 Bytes

自动生成 URL 字符串

写完博客都需要写一个访问地址作为唯一的阅读入口,每次写完都需要写一遍就会显得特别的麻烦。如果一件机械性的事件重复出现 2 次以上,就可以考虑是否有复用的方法,来简化这个过程。

最近正在学习 Python,就用 Python 写了一个随机生成字符串的函数。鉴于 26 个字符中的个别字符和其他的字母组合不太美观,仅选取了 A - F,0 - 9 这两段字符串的字符来生成。生成的结果类似:8d5590e49c02 这样的 12 位字符串。

自从使用了这个方法,再也不用为写博客的 URL 而短暂的苦恼,更加的简洁和方便。

from random import Random

# 生成 12 位随机 URL 地址
def randrom_url():
	url = ''
	chars = 'abcdef0123456789'
	length = len(chars)
	random = Random()
	for x in range(12):
		url += chars[random.randint(0,length - 1)]
	print (url)
	return url

if __name__ == '__main__':
    randrom_url()