Sometimes, while working with Python, we can have a problem in which we need to print the specific number of alphabets in order. This can have application in school-level programming. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + chr()
This is brute force way to perform this task. In this, we iterate the elements till which we need to print and concatenate the strings accordingly after conversion to the character using chr().
Python3
# Python3 code to demonstrate working of # Print Alphabets till N # Using loop # initialize N N = 20 # printing N print ("Number of elements required : " + str (N)) # Print Alphabets till N # Using loop res = "" for idx in range ( 97 , 97 + N): res = res + chr (idx) # printing result print ("Alphabets till N are : " + str (res)) |
Number of elements required : 20 Alphabets till N are : abcdefghijklmnopqrst
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using string.ascii_lowercase + slicing:
Combination of the above functionalities can also be used to perform this task. In this, we use an inbuilt function to get extract the lowercase string and extract the N characters using slicing.
Python3
# Python3 code to demonstrate working of # Print Alphabets till N # Using string.ascii_lowercase + slicing import string # initialize N N = 20 # printing N print (& quot Number of elements required : & quot + str (N)) # Print Alphabets till N # Using string.ascii_lowercase + slicing res = string.ascii_lowercase[:N] # printing result print (& quot Alphabets till N are : & quot + str (res)) |
Number of elements required : 20 Alphabets till N are : abcdefghijklmnopqrst
Time complexity: O(1), since the length of the input is constant and the program does not perform any loops or other operations that depend on the input size. The slicing operation used to extract the first N characters of the alphabet string is a constant-time operation.
Auxiliary space: O(1), since the program does not create any data structures to store the input or intermediate results. The only additional memory used is for the N variable, which is a constant-size integer, and the res variable, which stores a string of length N.
Method #3 : Using ord() and chr() functions
The Python ord() function converts a character into an integer that represents the Unicode code of the character. Similarly, the chr() function converts a Unicode code character into the corresponding string.
Python3
# Python3 code to demonstrate working of # Print Alphabets till N # Using loop # initialize N N = 20 # printing N print ( "Number of elements required : " + str (N)) # Print Alphabets till N # Using loop s = "" x = ord ( 'a' ) + N - 1 y = ord ( 'a' ) while (y< = x): s + = chr (y) y + = 1 # printing result print ( "Alphabets till N are : " + s) |
Number of elements required : 20 Alphabets till N are : abcdefghijklmnopqrst
Time complexity: O(N), The while loop runs N times to generate the string of alphabets, hence the time complexity of the program is O(N).
Auxiliary Space: O(1)
Method #4 : Using list comprehension and join
Python3
#Python3 code to demonstrate working of #Print Alphabets till N #Using list comprehension #initialize N N = 20 #printing N print ( "Number of elements required : " + str (N)) #Print Alphabets till N #Using list comprehension res = ' '.join([chr(i) for i in range(ord(' a '), ord(' a') + N)]) #printing result print ( "Alphabets till N are : " + res) |
Number of elements required : 20 Alphabets till N are : abcdefghijklmnopqrst
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using recursion
- Define a function that takes an integer N as input
- If N is less than or equal to 0, return an empty string
- If N is greater than 0, recursively call the function with N-1 as input
- Convert the ASCII code of the current value of N to its corresponding character using chr() function
- Concatenate the character with the result of the recursive call
- Return the final result
Python3
# Python3 code to demonstrate working of # Print Alphabets till N # Using recursion def print_alphabets(N): if N = = 0 : return '' else : return print_alphabets(N - 1 ) + chr ( 96 + N) # initialize N N = 20 # printing N print ( "Number of elements required : " + str (N)) # Print Alphabets till N # Using recursion res = print_alphabets(N) # printing result print ( "Alphabets till N are : " + str (res)) #This code is contributed by Vinay Pinjala. |
Number of elements required : 20 Alphabets till N are : abcdefghijklmnopqrst
Time complexity: O(N), where N is the input integer. The recursive function is called N times, and each call takes constant time.
Auxiliary Space: O(N), where N is the input integer. The space required by the function call stack is proportional to the input size, since each recursive call adds a new frame to the stack.
Method 6: Using the reduce() function from the functools module and string concatenation:
Algorithm:
- Initialize the variable N to the desired number of alphabets.
- Print the value of N.
- Use functools.reduce to iterate over the range of ASCII codes from 97 to 97 + N (where 97 is the ASCII code for lowercase ‘a’) and concatenate the corresponding characters to the result string.
- Print the resulting string of alphabets.
Python3
import functools # initialize N N = 20 # printing N print ( "Number of elements required : " + str (N)) # Print Alphabets till N # Using reduce res = functools. reduce ( lambda s, i: s + chr (i), range ( 97 , 97 + N), '') # printing result print ( "Alphabets till N are : " + str (res)) #This code is contributed by Jyothi Pinjala. |
Number of elements required : 20 Alphabets till N are : abcdefghijklmnopqrst
The time complexity : O(N), where N is the number of alphabets to print. This is because we iterate over a range of length N, and for each iteration, we perform a constant amount of work.
The auxiliary space: O(N), since we create a string of length N to hold the alphabets. This string is the only data structure that grows with input size, so the space complexity is proportional to N.