Create a string of N characters in one line in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Create a string of N characters in one line in Python #

Use the multiplication operator to create a string of N characters in one line, e.g. my_str = 'a' * 5. The multiplication operator will return a new string of length N by repeating the specified character N times.

main.py
import string import random # ✅ Create a string of the same character repeated N times my_str = 'a' * 5 print(my_str) # 👉️ aaaaa # ----------------------------------- # ✅ Create a string of random characters repeated N times my_str = ''.join( random.choice(string.ascii_lowercase) for _ in range(5) ) print(my_str) # 👉️ twgku

The first example uses the multiplication operator to create a string of N characters.

main.py
my_str = 'a' * 5 print(my_str) # 👉️ aaaaa print('a' * 2) # 👉️ 'aa' print('a' * 3) # 👉️ 'aaa'
When the multiplication operator is used with a string and an integer, it repeats the string the specified number of times.

Alternatively, you can use the random.choice() method.

Create a string of N characters in one line using random.choice() #

To create a string of N characters in one line:

  1. Use the string module to get a string of the ASCII characters.
  2. Use the random.choice() method to get N random characters from the string.
  3. Use the str.join() method to join the object into a string.
main.py
import string import random my_str = ''.join( random.choice(string.ascii_lowercase) for _ in range(5) ) print(my_str) # 👉️ twgku

We used the string module to get a string containing the lowercase ASCII characters.

main.py
import string # abcdefghijklmnopqrstuvwxyz print(string.ascii_lowercase) # ABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.ascii_uppercase) # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.ascii_letters)

The ascii_lowercase attribute returns a string containing the lowercase ASCII letters.

You can use the ascii_uppercase attribute if you need to get a string containing the uppercase ASCII letters or the ascii_letters attribute to get a string containing both.

The random.choice method takes a sequence and returns a random element from the non-empty sequence.

main.py
import string import random print(random.choice(string.ascii_lowercase)) # 👉️ m print(random.choice(string.ascii_lowercase)) # 👉️ w print(random.choice(string.ascii_lowercase)) # 👉️ y

The last step is to use the str.join() method to join the iterable into a string.

main.py
import string import random my_str = ''.join( random.choice(string.ascii_lowercase) for _ in range(5) ) print(my_str) # 👉️ twgku

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used an empty string to join the elements of the iterable into a string without a delimiter.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.