In my years of developing Python applications, I’ve often found myself needing to generate a specific sequence of characters for formatting or data padding.
Whether I was creating visual separators for a command-line tool or preparing fixed-width data for a financial report, knowing the quickest way to repeat a character is a handy trick.
In this tutorial, I will show you several easy ways to create a string of N characters in Python.
Use the String Multiplication Operator (*)
This is by far my favorite method because it is readable and incredibly fast. In Python, you can use the asterisk symbol to repeat a string a certain number of times.
I use this all the time when I need to create a simple visual divider in the console output.
Suppose you want to create a divider made of 50 hyphens for a shipping report for a warehouse in Chicago.
# Number of characters
n = 50
# Using the * operator
divider = "-" * n
print("Shipping Report - Chicago Hub")
print(divider)You can refer to the screenshot below to see the output.

In this example, Python takes the hyphen character and repeats it exactly 50 times. It’s clean and gets the job done instantly.
Use the join() Method with a List
Sometimes, I need to create a string by joining specific elements together. While multiplication is faster for a single character, the join() method is very flexible.
I often use this when I’m dealing with more complex logic where the character might change based on a condition, though here we will keep it simple.
Imagine you are generating a masked string for a Social Security Number (SSN) where you need to show only the last four digits.
# We want 5 masked characters for the beginning of an SSN
n = 5
mask_char = "*"
# Creating a list of N characters and joining them
masked_part = "".join([mask_char for _ in range(n)])
ssn_suffix = "1234"
print(f"Masked ID: {masked_part}-{ssn_suffix}")You can refer to the screenshot below to see the output.

Here, the list comprehension creates a list of five asterisks, and “”.join() merges them into a single string.
Use a For Loop (The Manual Way)
When I first started coding, I used loops for everything. While it isn’t the most “Pythonic” way to repeat a character, it helps you understand how strings are built through concatenation.
I don’t recommend this for large strings because it’s slower, but it works fine for small tasks.
Let’s say you want to create a string of 10 dollar signs to represent a “Luxury” rating for a hotel in New York City.
# Number of dollar signs
n = 10
rating_string = ""
# Manually appending in a loop
for i in range(n):
rating_string += "$"
print(f"Hotel Rating: {rating_string}")You can refer to the screenshot below to see the output.

Every time the loop runs, Python creates a new string object. This is why it’s less efficient than the multiplication method.
Use the ljust() or rjust() String Methods
If your goal is to create a string of a specific length by padding an existing string, ljust() (left justify) or rjust() (right justify) are perfect.
I find this extremely useful when generating aligned text tables or formatting invoices for US-based clients.
Suppose you have a product name like “California Almonds” and you want the total string length to be 30, filling the rest with dots.
text = "California Almonds"
total_length = 30
# Pad the string to the right with dots
formatted_string = text.ljust(total_length, ".")
print(formatted_string + " $12.99")You can refer to the screenshot below to see the output.

The ljust method ensures the string reaches the length of 30 by adding as many dots as necessary at the end.
Create a Random String of N Characters
There are times when I need a unique string rather than a repeating character, such as generating a random 8-character promo code for a marketing campaign in Texas.
For this, I use the string and random modules.
import random
import string
def generate_promo_code(n):
# Combine uppercase letters and digits
characters = string.ascii_uppercase + string.digits
return ''.join(random.choice(characters) for _ in range(n))
# Generate an 8-character random code
print(f"Your Promo Code: {generate_promo_code(8)}")This method is great because it picks a random character from the pool of letters and numbers N times and joins them together.
Performance Considerations
In my experience, if you just need to repeat a single character, always go with the multiplication operator (*).
It is implemented in C at the language level, making it significantly faster than loops or joining lists.
I’ve tested this with very large values of N, and the multiplication method consistently outperforms the others.
Common Use Cases in Python Development
Throughout my career, I’ve used these techniques for:
- Data Padding: Aligning columns in text-based CSV exports.
- Security: Masking sensitive information like credit card numbers.
- UI/UX: Creating progress bars or separators in CLI tools.
- Testing: Generating large strings to test the limits of a database field.
I hope you found this tutorial useful!
I have tried to cover the most practical ways to handle string creation so you can choose the one that fits your specific project needs.
You may also like to read:
- Remove Item from Set in Python
- Check if a Python Set is Empty
- Add Item to Set in Python
- Create an Empty Set in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.