python|September 14, 2018|1 min read

Python: How to generate string of arbitrary length of any alphabet characters

TL;DR

Use Python's string multiplication operator ('a' * 300) to generate repeated character strings, or use random.choices() with string.ascii_letters for random strings of any length.

Python: How to generate string of arbitrary length of any alphabet characters

I was testing a bug where a field was limited to 255 characters only. I needed to generate a string of more than 255 characters.

The simplest code I found:

# Generate string of only character 'a' 300 times
'a' * 300

This will generate a string of 300 characters

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa......

Or, you can mix characters

'a' * 100 + 'b' * 100 + 'hey master'

Function to Generate strings of arbitrary length

def gen(character, length):
    return character * length

# to use
>>> gen('a',20)
'aaaaaaaaaaaaaaaaaaaa'
>>> gen('a',20) + gen('b',10)
'aaaaaaaaaaaaaaaaaaaabbbbbbbbbb'

Use Random Number library to generate arbitrary string

Using module random

import string
import random

# for upper case ascii characters, and 10 characters long
''.join(random.choice(string.ascii_uppercase) for _ in range(10))

# In a method
def generate(characters=string.ascii_uppercase, length = 10):
    return ''.join(random.choice(characters) for _ in range(length))

>>> generate()
'GSPUYMYMLB'

# For having upper case as well as numerics
>>> generate(characters=string.ascii_uppercase + string.digits)
'X9O814PS58'

# For longer string
>>> generate(characters=string.ascii_uppercase + string.digits, length=20)
'7HCYBJAQP9N48TC179Q8'

Related Posts

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Implementation of Timeit function, Get performance numbers for a function

Implementation of Timeit function, Get performance numbers for a function

This is regarding the timeit implementation in python. The basic requirement…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Python Code - How To Read CSV with Headers into an Array of Dictionary

Python Code - How To Read CSV with Headers into an Array of Dictionary

Introduction Lets assume we have a csv something similar to following: Python…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…